简体   繁体   English

在JavaScript中将JSON对象添加到多维数组

[英]Add JSON object to multidimensional array in javascript

My aim is to add a JSON object based on certain conditions to an array which is then to be used to construct a WINJSList. 我的目标是将基于某些条件的JSON对象添加到数组中,然后将其用于构造WINJSList。 I'm really struggling with accessing the elements of the list OR array after I have used the array.push method. 在使用array.push方法之后,我真的很难访问列表OR数组的元素。 I wanted to access these elements to ensure I am doing the addition right. 我想访问这些元素,以确保自己做得正确。 Any help would be greatly appreciated. 任何帮助将不胜感激。 I have the following code 我有以下代码

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]);

I also tried: 我也尝试过:

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);

In the console I either get undefined or [object object] 在控制台中,我要么未定义,要么[对象对象]

For WinJS.Binding.List, refer documention here . 对于WinJS.Binding.List,请参考此处的文档。

There are couple of issues with code: 代码有几个问题:

  • WinJS List initializes itself with the array passed to the constructor. WinJS List使用传递给构造函数的数组进行初始化。 After that if the array is modified, changes do not propogate to the winjs list. 此后,如果修改数组,则更改不会传播到winjs列表。
  • To access element at index, use list.getAt(index) 要访问索引处的元素,请使用list.getAt(index)

this code should work: 该代码应该工作:

var data = []; 
data.push({
    name: "Joe Dowling",
    image: "image/Joe Dowling.png",
    ClientID: "1234"
});
data.push({
    name: "Esteban Flamenco ",
    image: "image/Esteban Flamenco.png",
    ClientID: "6666"
        });
data.push({
    name: "Plain Jane ",
    image: "image/Plain Jane.png",
    ClientID: "0000"
});

var list = new WinJS.Binding.List(data);
var item = list.getAt(0);
console.info(JSON.stringify(item))

You are making this harder than what it should be here is a simple example based on the data you are using : 您正在做的事情比应该做的要困难,这是一个基于您正在使用的数据的简单示例:

var data = []; 
data.push({ 
      name: "Joe Dowling", 
      image: "image/Joe Dowling.png", 
      ClientID: "1234" 
    });
data.push({
      name: "Esteban Flamenco ", 
      image: "image/Esteban Flamenco.png", 
      ClientID: "6666" 
    });
data.push({ 
      name: "Plain Jane ", 
      image: "image/Plain Jane.png", 
      ClientID: "0000" 
    });


<!-- var item = data.getAt(0) -->
console.info(JSON.stringify(data))
console.info(JSON.stringify(data[0]))
var c = data[0];

for(var i in c){
    console.log(i); //key
    console.log(c[i]); //value
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM