jquery grep json 对象数组
我尝试使用 grep 过滤 json 对象数组,以便搜索该数组,如果键 #2-6 中任何一个的值为 yes,则返回键 1 和 7 的值。
该数组如下 - 换句话说,如果“位置”键的任何值为“是”,则名称和描述将作为列表项返回。
非常感谢任何帮助。
[
{
"name": "name",
"location1": "no",
"location2": "no",
"location3": "yes",
"location4": "no",
"location5": "no",
"description": "description of services"
},
{
"name": "name",
"location1": "yes",
"location2": "no",
"location3": "yes",
"location4": "no",
"location5": "no",
"description": "description of services"
}
]
Im trying to use grep to filter a json object array so that the array is searched and if the value of any of keys #2-6 are yes, the value of keys 1 and 7 are returned.
The array is below -- in other words, if any of values for the 'location' keys are yes, the name and description are returned as list items.
Any help is VERY much appreciated.
[
{
"name": "name",
"location1": "no",
"location2": "no",
"location3": "yes",
"location4": "no",
"location5": "no",
"description": "description of services"
},
{
"name": "name",
"location1": "yes",
"location2": "no",
"location3": "yes",
"location4": "no",
"location5": "no",
"description": "description of services"
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要同时使用
grep
和map
。如果a
是上述数组(但具有name1
、name2
等),则在以下内容之后:c
将包含[{"name":"name1","description":"服务描述 1"},{"name":"name2","description":"服务描述 2"}]
<一href="http://jsfiddle.net/7zHP3/" rel="noreferrer">参见示例→
You will need to use both
grep
andmap
. Ifa
is the array described above (but withname1
,name2
, etc), then after the following:c
will contain[{"name":"name1","description":"description of services1"},{"name":"name2","description":"description of services2"}]
See example →
我的版本与之前的答案非常相似,希望对您有所帮助:
My version is very similar to previous answer, I hope it helps: