Arrays 如何过滤再次包含JSON对象数组的JSON对象数组?
我想从示例json对象中获取名称,其中workLocation为“city 1”,我如何才能做到这一点 我知道我们可以使用下划线或lodash来过滤json对象数组,比如var filteredata=389;。where(jsonData,{“id”:1}),但不确定如何从特定的工作位置获取名称Arrays 如何过滤再次包含JSON对象数组的JSON对象数组?,arrays,node.js,json,underscore.js,lodash,Arrays,Node.js,Json,Underscore.js,Lodash,我想从示例json对象中获取名称,其中workLocation为“city 1”,我如何才能做到这一点 我知道我们可以使用下划线或lodash来过滤json对象数组,比如var filteredata=389;。where(jsonData,{“id”:1}),但不确定如何从特定的工作位置获取名称 [ { "id": 1, "name": "John", "age": 12, "data": [ { "worklocatio
[
{
"id": 1,
"name": "John",
"age": 12,
"data": [
{
"worklocation" : "city 1",
"pin" : "909"
},
{
"worklocation" : "city 2",
"pin" : "808"
}
]
},
{
"id": 2,
"name": "Shawn",
"age": 22,
"data": [
{
"worklocation" : "city 3",
"pin" : "608"
},
{
"worklocation" : "city 4",
"pin" : "508"
}
]
}
]
我期望{“name”:“Shawn”}的输出,当我使用city 3进行筛选时,您必须迭代两个数组,并比较是否可能找到具有相同ID的
工作位置。我为您创建了一个示例
const数据=[
{
“id”:1,
“姓名”:“约翰”,
“年龄”:12岁,
“数据”:[{
“工作地点”:“城市1”,
“pin”:“909”
},
{
“工作地点”:“城市2”,
“pin”:“808”
}
]
},
{
“id”:2,
“姓名”:“肖恩”,
“年龄”:22岁,
“数据”:[{
“工作地点”:“城市3”,
“pin”:“608”
},
{
“工作地点”:“城市4”,
“pin”:“508”
}
]
}
]
const findName=(列表,id)=>{
const found=list.find(项=>{
const foundCity=item.data.find(worklocationItem=>worklocationItem.worklocation==id)
返回!!foundCity
})
如果(找到){
返回find.name
}
返回空
}
const el=document.getElementById('data'))
const cityID=‘城市3’;
el.innerHTML=findName(数据,cityID)
console.log(findName(data,cityID))