gpt4 book ai didi

javascript - Lodash:差异函数,但基于 JSON 格式

转载 作者:行者123 更新时间:2023-12-01 02:25:40 25 4
gpt4 key购买 nike

var b = ["text1", "text2"];
var a = [
{name: "text3", value: 2},
{name: "text4", value: 7},
{name: "text1", value: 4}
];

我尝试过多种Lodash函数,但没有一个返回我想要实现的目标。

我想要的是:

var c = ["text1"]; // uniques from a compared to b
var d = [
{name: "text3", value: 2},
{name: "text4", value: 7}
]; // uniques from b compared to b

最佳答案

您可以过滤数组并推送不唯一的项目。

var b = ["text1", "text2"],
a = [{ name: "text3", value: 2 }, { name: "text4", value: 7 }, { name: "text1", value: 4 }],
c = [],
d = a.filter(({ name }) => !b.includes(name) || !c.push(name));

console.log(c);
console.log(d);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - Lodash:差异函数,但基于 JSON 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48833825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com