[英]Accessing and searching data from a JSON file
If I have the JSON object below 如果我下面有JSON对象
{
"animals":{
"a_1":{
"species":"Tiger",
"name":"Timmy",
"dob":"2016-03-12",
},
"a_2":{
"species":"Lion",
"name":"Leo",
"dob":"2012-12-04",
}
}
}
How do I search to return 如何搜寻退货
"a_1": {
"species":"Tiger",
"name":"Timmy",
"dob":"2016-03-12",
},
for the search a_1
? 搜索
a_1
?
Is there a similar way to call a_1
/ a_2
, etc? 有没有类似的方法来调用
a_1
/ a_2
等?
I think you should change your data structure to an array. 我认为您应该将数据结构更改为数组。 It would be easier:
这样会更容易:
const data = { "animals": { "a_1": { "species": "Tiger", "name": "Timmy", "dob": "2016-03-12", }, "a_2": { "species": "Lion", "name": "Leo", "dob": "2012-12-04", } } } const animalList = Object.keys(data.animals).map(key => ({ key, item: data.animals[key] })) const searchName = input => animalList.filter(animal => animal.item.name === input); console.log(searchName('Leo'))
You can directly search by Key. 您可以直接通过键进行搜索。
For example: 例如:
var data = {
"animals":{
"a_1":{
"species":"Tiger",
"name":"Timmy",
"dob":"2016-03-12",
},
"a_2":{
"species":"Lion",
"name":"Leo",
"dob":"2012-12-04",
}
}
};
var searchKey = "a_1";
console.log(data["animals"][searchKey]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.