作者:华力 | 来源:互联网 | 2023-09-18 20:34
如何解析以下示例json并创建名称为allen或tom或jack的另一个json。
名称可以出现多次。
{
"responseCode": 0,"responseMessage": "SUCCESS","data": [{
"name": "tom","school": "abc","college": "def"
},{
"name": "harry",{
"name": "jack",{
"name": "tom",{
"name": "allen","college": "def"
}
...
...
]
}
尝试使用Array.prototype.filter
:
const data = `{
"responseCode": 0,"responseMessage": "SUCCESS","data": [
{
"name": "tom","school": "abc","college": "def"
},{
"name": "harry",{
"name": "jack",{
"name": "tom",{
"name": "allen","college": "def"
}
]
}`
const dataObject = JSON.parse(data);
const filteredResults = dataObject.data.filter(function (entry) {
return ['tom','jack','allen'].indexOf(entry.name) !== -1
})
console.log(filteredResults);