Add JSON object to multidimensional array in javascript
13,856
For WinJS.Binding.List, refer documention here.
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)
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))
Author by
user2363025
Updated on June 05, 2022Comments
-
user2363025 almost 2 years
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
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]