我的目标是将基于某些条件的JSON对象添加到一个数组中,然后该数组用于构造WINJSList。在使用array.push方法之后,我真的很难访问列表OR数组的元素。我想访问这些元素以确保我正在进行添加。任何帮助将不胜感激。我有以下代码
var names_Array = new Array;
var names_List = new WinJS.Binding.List(names_Array);
if (condition) {
if (condition) {
names_List.push({ name: "Joe Dowling", image: "image/Joe Dowling.png", ClientID: "1234" });
}
else if (condition) {
names_List.push({ name: "Esteban Flamenco ", image: "image/Esteban Flamenco.png", ClientID: "6666" });
}
else if (condition) {
names_List.push({ name: "Plain Jane ", image: "image/Plain Jane.png", ClientID: "0000" });
}
console.log(names_Array);
console.log(names_Array[0]);
console.log(names_List);
console.log(names_List[0]);
我也尝试过:
var names_Array = new Array; if (condition) {
if (condition) {
names_Array.push({ name: "Joe Dowling", image: "image/Joe Dowling.png", ClientID: "1234" });
}
else if (condition) {
names_Array.push({ name: "Esteban Flamenco ", image: "image/Esteban Flamenco.png", ClientID: "6666" });
}
else if (condition) {
names_Array.push({ name: "Plain Jane ", image: "image/Plain Jane.png", ClientID: "0000" });
}
var names_List = new WinJS.Binding.List(names_Array);
在控制台中,我得到未定义或[对象对象]
答案 0 :(得分:1)
我认为你应该像这样声明你的names_Array:
var names_Array = [];
另外,使用switch语句有助于清除一些混淆,我不认为第一个if语句有一个右括号(来自我的观察)