用数组 javascript 过滤掉数组
filter out array with array javascript
如何过滤掉对象数组 a
,其中 item1 是数组 b
的一部分?
a = [
{
"item1": "apple",
"item2": "banana"
},
{
"item1": "apple",
"item2": "cherry"
},
{
"item1": "melon",
"item2": "cherry"
},
{
"item1": "banana",
"item2": "melon"
}]
b = [ "apple", "banana"]
预期的数组将是:
expected_array = [{
"item": "melon",
"item2": "cherry"
}]
我试过:
a.filter(el => !b.includes(el.item1))
但生成的数组与原始数组的长度相同。
我在 Node Red 中执行此操作,这是我的测试设置:
[{"id":"ab67374c.c64458","type":"debug","z":"42edc0b9.7ba91","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2040,"y":300,"wires":[]},{"id":"3fbdbc18.951c94","type":"function","z":"42edc0b9.7ba91","name":"cycle counter","func":"\n\nmsg.payload = msg.payload.filter(el => !msg.exclude.includes(el.altname));\n\nnode.warn(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1810,"y":320,"wires":[["ab67374c.c64458"]]},{"id":"b4b2e91f.8e4368","type":"inject","z":"42edc0b9.7ba91","name":"","props":[{"p":"payload"},{"p":"exclude","v":"[[\"melon\"],[\"banana\"]]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"altname\":\"melon\"},{\"altname\":\"cherry\"},{\"altname\":\"banana\"}]","payloadType":"json","x":1650,"y":320,"wires":[["3fbdbc18.951c94"]]}]
正如其他人在评论中所说,您的代码很好,但您需要将过滤器函数的输出分配给一个变量。
let a = [{
"item1": "apple",
"item2": "banana"
},
{
"item1": "apple",
"item2": "cherry"
},
{
"item1": "melon",
"item2": "cherry"
},
{
"item1": "banana",
"item2": "melon"
}
];
let b = ["apple", "banana"];
// assign filter output to variable `c`
let c = a.filter(el => !b.includes(el.item1));
console.log(c); // your output
如何过滤掉对象数组 a
,其中 item1 是数组 b
的一部分?
a = [
{
"item1": "apple",
"item2": "banana"
},
{
"item1": "apple",
"item2": "cherry"
},
{
"item1": "melon",
"item2": "cherry"
},
{
"item1": "banana",
"item2": "melon"
}]
b = [ "apple", "banana"]
预期的数组将是:
expected_array = [{
"item": "melon",
"item2": "cherry"
}]
我试过:
a.filter(el => !b.includes(el.item1))
但生成的数组与原始数组的长度相同。
我在 Node Red 中执行此操作,这是我的测试设置:
[{"id":"ab67374c.c64458","type":"debug","z":"42edc0b9.7ba91","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2040,"y":300,"wires":[]},{"id":"3fbdbc18.951c94","type":"function","z":"42edc0b9.7ba91","name":"cycle counter","func":"\n\nmsg.payload = msg.payload.filter(el => !msg.exclude.includes(el.altname));\n\nnode.warn(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1810,"y":320,"wires":[["ab67374c.c64458"]]},{"id":"b4b2e91f.8e4368","type":"inject","z":"42edc0b9.7ba91","name":"","props":[{"p":"payload"},{"p":"exclude","v":"[[\"melon\"],[\"banana\"]]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"altname\":\"melon\"},{\"altname\":\"cherry\"},{\"altname\":\"banana\"}]","payloadType":"json","x":1650,"y":320,"wires":[["3fbdbc18.951c94"]]}]
正如其他人在评论中所说,您的代码很好,但您需要将过滤器函数的输出分配给一个变量。
let a = [{
"item1": "apple",
"item2": "banana"
},
{
"item1": "apple",
"item2": "cherry"
},
{
"item1": "melon",
"item2": "cherry"
},
{
"item1": "banana",
"item2": "melon"
}
];
let b = ["apple", "banana"];
// assign filter output to variable `c`
let c = a.filter(el => !b.includes(el.item1));
console.log(c); // your output