0

我有两个数组,我正在比较两个值,然后设置一个 json 对象:

var compare = ["hh", "pictures", "videos", "aboutMe", "contactMe", "cat", "location"];
var data = ["pictures", "videos", "aboutMe", "contactMe", "cat", "location"];
    for (var j=0; j<compare.length; j++) {
        if (compare[j] === data[j]) {
            self.MenuItems.menu_item[j].added = "added";
        }else if (compare[j] !== data[j]){
            self.MenuItems.menu_item[j].added = "";
        }
    }

出于某种原因,对于 all self.MenuItems.menu_item[j],它们都等于 "add" 或 "";...

4

1 回答 1

1

只是让您知道这种情况不是很有效,这里应该如何

if (compare[j] === data[j]) {
    self.MenuItems.menu_item[j].added = "added";
}else {
    self.MenuItems.menu_item[j].added = "";
}

并且代码执行预期的操作。除非你有别的想法。

于 2012-07-27T19:28:22.403 回答