我有两个数组,我正在比较两个值,然后设置一个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 = "";
}
}
出于某种原因,对于所有self.MenuItems.menu_item[j]
,它们都等于“已添加”或“”; ...
答案 0 :(得分:1)
只是你知道这个条件不是很有效,这里应该如何
if (compare[j] === data[j]) {
self.MenuItems.menu_item[j].added = "added";
}else {
self.MenuItems.menu_item[j].added = "";
}
并且代码执行预期的操作。除非你有别的想法。