将 JSON 对象添加到 javascript 中的多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16811145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:04:55  来源:igfitidea点击:

Add JSON object to multidimensional array in javascript

javascriptarraysjsonobjectwinjs

提问by user2363025

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. I'm really struggling with accessing the elements of the list OR array after I have used the array.push method. 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

我的目标是将基于特定条件的 JSON 对象添加到数组中,然后该数组将用于构建 WINJSList。在使用 array.push 方法后,我真的很难访问列表或数组的元素。我想访问这些元素以确保我正确地进行加法。任何帮助将不胜感激。我有以下代码

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]

在控制台中,我要么得到 undefined 要么 [object object]

回答by Sushil

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. After that if the array is modified, changes do not propogate to the winjs list.
  • To access element at index, use list.getAt(index)
  • WinJS List 使用传递给构造函数的数组初始化自身。之后,如果数组被修改,更改不会传播到 winjs 列表。
  • 要访问索引处的元素,请使用 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))

回答by grepit

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
}