create json array in javascript code example
Example 1: json example array
{
"name":"John",
"age":30,
"cars":[ "Ford", "BMW", "Fiat" ]
}
{
"people": [
{
"website": "stackabuse.com",
"from": "Nebraska",
"name": "Scott"
},
{
"website": "google.com",
"from": "US",
"name": "Zuck"
}
]
}
Example 2: json arrays
{
"array": [{1,2,3,4,5,6}]
}
Example 3: js create json array
var employees = {
accounting: []
};
for(var i in someData) {
var item = someData[i];
employees.accounting.push({
"firstName" : item.firstName,
"lastName" : item.lastName,
"age" : item.age
});
}
Example 4: javascript create json object from array
var array = ['a', 1, 'b', 2, 'c', 3],
object = {},
i
for (var i = 0; i < array.length; i += 2) {
object[array[i]] = array[i + 1];
}
console.log(object);
Example 5: hwo to access arary in json
console.log(dataJS);
console.log(dataJS[0].eee1);
console.log(dataJS[0].comments[0]);