我有一套id。
let ids = ["5ae03c56dc0e82410d2746d7", "5ae05bf146e0fbeeb1869d7b", "5ae03c56dc0e82fd2f2746d6", "5ae04788c9e77cb0a03b3228", "5adefec246e0fb1c23f5ba6b", "5ae8498edc0e82c7cbad5026", "5ae84b4fc9e77c5db6fbf3f8"]
我还有一组如下所示的数据。
{
"interactions":[
{
"author":{
"id":"10158567383880542",
"name":"Stephen Wilson"
},
"meta":{
"tags":[
{
"id":"5ae04788c9e77cb0a03b3228"
},
{
"id":"5ae04788c9e77cb0a03b365"
}
]
}
}
]
}
在interaction数组中有多个对象。有一个名为" meta"的属性,在meta中有一个类型为array的属性" tags"。
我想匹配ids数组来查找并返回meta.tags.id等于任何id的任何交互。
let matches = _(interactions)
.keyBy('meta.tags.id')
.at(ids)
.value();
这里尝试的问题是meta.tags是一个数组。
答案 0 :(得分:2)
您可以合并Array.prototype.filter(),Array.prototype.some()和Array.prototype.includes()
代码:
const ids = ["5ae05bf146e0fbeeb1869d7b", "5ae03c56dc0e82fd2f2746d6"]
const data = {"interactions":[{"author":{"id":"10158567383880542","name":"Stephen Wilson"},"meta":{"tags":[{"id":"5ae04788c9e77cb0a03b3228"},{"id":"5ae04788c9e77cb0a03b365"}]}},{"author":{"id":"10158567383880543","name":"Phil Murray"},"meta":{"tags":[{"id":"5ae05bf146e0fbeeb1869d7b"},{"id":"5ae04788c9e77cb0a03b369"}]}},{"author":{"id":"10158567383880543","name":"Steve Jobs"},"meta":{"tags":[{"id":"5ae84b4fc9e77c5db6fbf3f8"},{"id":"5ae04788c9e77cb0a44469"}]}},{"author":{"id":"10158567383880543","name":"John Connor"},"meta":{"tags":[{"id":"5ae84b4fc9e778c5db6fbf3f8"},{"id":"5ae04788c94577cb0a44469"}]}}]};
const result = data.interactions.filter(i => i.meta.tags.some(t => ids.includes(t.id)));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
不需要单独的库。您可以在此处使用直接javascript过滤和映射:
const ids = ["5ae03c56dc0e82410d2746d7", "5ae05bf146e0fbeeb1869d7b", "5ae03c56dc0e82fd2f2746d6", "5ae04788c9e77cb0a03b3228", "5adefec246e0fb1c23f5ba6b", "5ae8498edc0e82c7cbad5026", "5ae84b4fc9e77c5db6fbf3f8"]
const inters = {
"interactions": [{
"author": {
"id": "10158567383880542",
"name": "Stephen Wilson"
},
"meta": {
"tags": [{
"id": "5ae04788c9e77cb0a03b3228"
}, {
"id": "5ae04788c9e77cb0a03b365"
}]
}
},
{
"author": {
"id": "10158567383880543",
"name": "Phil Murray"
},
"meta": {
"tags": [{
"id": "5ae05bf146e0fb345eeb1869d7b"
}, {
"id": "5ae04788c9e3477cb0a03b369"
}]
}
},
{
"author": {
"id": "10158567383880543",
"name": "Steve Jobs"
},
"meta": {
"tags": [{
"id": "5ae8498edc0e82c7cbad5026"
}, {
"id": "5ae84b4fc9e77c5db6fbf3f8"
}]
}
},
{
"author": {
"id": "10158567383880543",
"name": "John Connor"
},
"meta": {
"tags": [{
"id": "5ae8498edc0e82c7cbad5026"
}, {
"id": "5ae84b4fc9e77c5db6fbf3f8"
}]
}
}
]
};
const matching = ids
.map(
id => inters.interactions.filter(interaction => interaction.meta.tags.some(tag => tag.id === id))
)
.filter(item => item.length > 0)
.reduce((flattened, item) => {
return flattened.concat(item);
}, []);
console.log(matching);
答案 2 :(得分:0)
过滤就足够了。不需要图书馆
const ids = ["5ae03c56dc0e82410d2746d7", "5ae05bf146e0fbeeb1869d7b", "5ae03c56dc0e82fd2f2746d6", "5ae04788c9e77cb0a03b3228", "5adefec246e0fb1c23f5ba6b", "5ae8498edc0e82c7cbad5026", "5ae84b4fc9e77c5db6fbf3f8"]
const inter = { "interactions":[
{ "author":{ "id":"10158567383880542", "name":"Stephen Wilson 1" }, "meta":{ "tags":[ { "id":"5ae04788c9e77cb0a03b3228" }, { "id":"5ae04788c9e77cb0a03b365" } ] } },
{ "author":{ "id":"10158567383880542", "name":"Stephen Wilson 2" }, "meta":{ "tags":[ { "id":"xxx" }, { "id":"yyy" } ] } },
{ "author":{ "id":"10158567383880542", "name":"Stephen Wilson 3" }, "meta":{ "tags":[ { "id":"5ae03c56dc0e82fd2f2746d6" }, { "id":"5ae84b4fc9e77c5db6fbf3f8" }
] } } ] }
let found = [];
inter.interactions.forEach(function(act,index) {
let actFound = act.meta.tags.filter(function(item) { return ids.indexOf(item.id) !=-1 });
if (actFound.length>0) found = found.concat(inter.interactions[index]); // or use ...spread
})
console.log(found)