フロントエンド側でのJSON取り回しサンプル
// JSONデータを定義
const jsonSample = [
{ "name": "john", "age": "28", "country": "united states" },
{ "name": "pierre", "age": "25", "country": "france" },
{ "name": "wang", "age": "37", "country": "china" }
];
console.log("original object: ", jsonSample);
// JSON要素を追加
jsonSample.push({"name": "patrick", "age": "33", "country": "united kingdom"})
console.log("pushed: ", jsonSample);
// JSON要素を削除
// 先頭から削除
jsonSample.shift();
// 末尾から削除
jsonSample.pop();
console.log("popped: ", jsonSample);
// JSONデータのフィルタリング
var jsonFiltered = jsonSample.filter(function (j){
return j.country != "china";
});
console.log("filtered by country is not 'china': ", jsonFiltered);
// JSONプロパティの削除
var jsonSlimed = jsonFiltered;
for(let i = 0; i < jsonSlimed.length; i++){ delete jsonSlimed[i].age; }
console.log("property age deleted: ", jsonSlimed);