JavaScriptでjson形式のデータにおいて特定の値を取り出す方法
方法1
var json = [{“id":1,"name":"大崎"},
{“id":2,"name":"品川"}
{“id":3,"name":"大井町"}];
var data = getJsonById(2,json);
function getJsonById(id,data){
for (var i = 0; i < data.length; i++) {
if (data[i].id==id) {
return data[i];
}
};
}
方法2
var json = [{“id":1,"station":"大崎"},
{“id":2,"name":"五反田"},
{“id":3,"name":"目黒"}
];
var data = json.find(function(e){return e.id == 1});
console.log(data);