【发布时间】:2017-08-26 00:01:07
【问题描述】:
我想在 jQuery 或 vanilla JavaScript 中过滤我的 JSON 对象。我更喜欢 vanilla JavaScript,但不知道如何解决这个问题。我创建了伪代码来解释我想要实现的目标。有什么想法吗?
我基本上只想允许不包含列表中的值的记录。第一个列表仅包含应从 expJSON 中排除的 mfs 值。第二个列表包含应该从 expJSON 中排除的 pn 值,依此类推...
伪代码
var results = $(jsonData).filter(function (i, n) {
return if list1 is not empty: (n.psn.indexOf(item1 from list1) === -1 || n.psn.indexOf(item2 from list1) === -1 || n.psn.indexOf(item3 from list1) === -1 ... ) &&
if list2 is not empty: (n.pn.indexOf(item1 from list2) === -1 || n.pn.indexOf(item2 from list2) === -1 || n.pn.indexOf(item3 from list2) === -1 ... ) &&
if list3 is not empty: (n.mft.indexOf(item1 from list3) === -1 || n.mft.indexOf(item2 from list3) === -1 || n.mft.indexOf(item3 from list3) === -1 ... ) &&
if list4 is not empty: (n.sl.indexOf(item1 from list4) === -1 || n.sl.indexOf(item2 from list4) === -1 || n.sl.indexOf(item3 from list4) === -1 ... ) &&
if list5 is not empty: (n.vtv.indexOf(item1 from list5) === -1 || n.vtv.indexOf(item2 from list5) === -1 || n.vtv.indexOf(item3 from list5) === -1 ... )
})
jsonData
[{"mft": "asjfasdf", "pn": "234awefwa", "vtv": "No", "psn": "234fasdfa", "sl": "asf8sf"}, {"mft": "fsjldfd98sf9d", "pn": "skfjsdf7df", "vtv": "Yes", "psn": "tfs76fdfd", "sl": "basd7f"}, {"mft": "fbsdf8df", "pn": "898723923", "vtv": "No", "psn": "fs7daf6sd", "sl": "f7s6df"}, {"mft": "sdf7688sdf76f", "pn": "131d21", "vtv": "Yes", "psn": "t23yt342y23", "sl": "bfldk34"} ...]
list1、list2、list3、list4、list5
item1 item2 item3
list1/mft = ["word1", "word2", "word3", ...]
list2/pn = ["word1", "word2", "word3", ...]
list3/vtv = ["word1", "word2", "word3", ...]
list4/psn = ["word1", "word2", "word3", ...]
list5/sl = ["word1", "word2", "word3", ...]
【问题讨论】:
-
我有点不清楚你想要什么,你能否让你的 jsonData 与你的列表匹配,以便我们可以准确地看到之前/之后?
-
我想显示 jsonData 中不包含列表值的记录。每个列表都包含 json 记录中键的值。
-
我已更新列表名称
-
每个对象总是有相同的键吗?每个对象都会有 mft、pn、vtv、psn 和 sl 吗?
-
可以,但列表可以为空
标签: javascript jquery json