Content
- Convert array to JSON
- Parse JSON (convert from JSON to array)
- Count JSON array length
- Do loop with JSON array
- Get data from JSON array
- Add item to JSON array
- Remove item from JSON array
Convert array to JSON
var myArr = {
key: value,
key2: value2
};
var myJSON = JSON.stringify(myArr);
Result:
'{"key":"value","key2":"value2"}';
Parse JSON (convert from JSON to array)
var myJSON = '{"key":"value","key2":"value2"}';
var myArr = JSON.parse(myJSON);
Result:
{
key: value,
key2: value2
};
Count JSON array length
var myJSON = '{"key":"value","key2":"value2"}';
var myArr = JSON.parse(myJSON);
var jsonLength = myArr.length;
Result:
2
Do loop with JSON array
var myJSON = '{"key":"value","key2":"value2"}';
var myArr = JSON.parse(myJSON);
myArr.forEach(function(nowItem) {
// Do things here
})
Get data from JSON array
var myJSON = '{"key":"value","key2":"value2"}';
var myArr = JSON.parse(myJSON);
console.log(myArr["key2"]);
Result:
value2
Add item to JSON array
var myJSON = '{"key":"value","key2":"value2"}';
var myArr = JSON.parse(myJSON);
myArr["key3"] = "value3";
myArr result:
{
key: "value",
key2: "value2",
key3: "value3"
}
Remove item from JSON array
var myJSON = '{"key":"value","key2":"value2"}';
var myArr = JSON.parse(myJSON);
delete myArr["key2"];
myArr result:
{
"key": "value"
}
Top comments (0)