2016-07-03 2 views
1

NW.js을 사용하는 부 프로젝트로 IDE에서 작업 중이고 포커스가있을 때 프로젝트 트리에 변경 사항이 있는지 감지해야합니다.NW.js : 두 배열이 동일하지 않은 경우

프로젝트 폴더에있는 파일의 포커스가 변경된 이후에 변경되었는지 확인하려면 JQuery's .each을 사용하고 있습니다.

다음은 윈도우 포커스 기능에 대한 내용입니다.

win.on("focus", function() { 
    var stringArray = []; 
    listFiles = fs.readdirSync(__dirname + "/content/project"); 
    console.log("checking if there's any changes"); 
    $.each($("[data-nme]"), function() { 
    stringArray.push(this.textContent); 
    }); 
    var array1 = listFiles; 
    var array2 = JSON.stringify(stringArray); 

    console.log(array1); 
    console.log(array2); 

    if (array1.toString() != array2.toString()) { 
    console.log("there's been a change"); 
    } 
}); 

다음과 같은 결과가 나타납니다. 그것이 동일한의 배열에 차이가 말한다 이유

enter image description here

는 이해가 안 돼요.

답변

1

오류는 평등을 확인하는 방식입니다. 하나는 배열의 JSON.stringified 다른 않습니다. JSON.stringify (stringArray)를 사용하여 array2를 정의하지 마십시오.

var foo = ["bar"]; 
foo.toString(); 

출력 : "foo"

JSON.stringify(foo).toString() 

출력 : "["foo"]"이 조건이 작동해야

(테스트하지 않았습니다) :

if (array1.toString() != stringArray.toString()) { 
    console.log("there's been a change"); 
    } 

당신의 최선의 선택을하려는 경우 배열을 비교해 보면 다음 중 하나를 확인하십시오 : How to compare arrays in JavaScript?

관련 문제