英文:
React JS comparing a value from two different array of objects
问题
我正在寻找一个简单的解决方案,只比较两个不同对象数组中的“id”值和“type”,如下所示:
var arr_a = [
{ id: 1, name: "thumbler", type: "add" },
{ id: 2, name: "spoon", type: "add" },
{ id: 3, name: "plate", type: "add" },
];
var arr_b = [
{ id: 1, name: "thumbler", type: "remove" },
{ id: 2, name: "spoon", type: "add" },
{ id: 3, name: "plate", type: "add" },
{ id: 4, name: "fork", type: "add" },
];
我想要的结果是:
var arr_c = [
{ id: 1, name: "thumbler", type: "remove" },
{ id: 4, name: "fork", type: "add" },
];
我尝试使用filter和some([来自这里][1]),但不起作用:
var arr_c = arr_b.filter(
(x) =>
arr_a.filter((y) => y.id === x.id && y.type === x.type)
.length > 0
);
有什么建议吗?
[1]: https://stackoverflow.com/questions/67863131/compare-2-two-different-arrays-and-show-the-object-with-the-same-value-with-js
英文:
I am looking for a simple solution to compare just the "id" values and "type" in from two different arrays of objects as the following:
var arr_a = [
{ id: 1, name: "thumbler", type: "add" },
{ id: 2, name: "spoon", type: "add" },
{ id: 3, name: "plate", type: "add" },
];
var arr_b = [
{ id: 1, name: "thumbler", type: "remove" },
{ id: 2, name: "spoon", type: "add" },
{ id: 3, name: "plate", type: "add" },
{ id: 4, name: "fork", type: "add" },
];
and I wanna a result like this:
var arr_c = [
{ id: 1, name: "thumbler", type: "remove" },
{ id: 4, name: "fork", type: "add" },
];
I tried to use filter and some (from here) but doesn't work:
var arr_c = arr_b.filter(
(x) =>
arr_a.filter((y) => y.id === x.id && y.action_type === x.action_type)
.length > 0
);
Any suggestions?
答案1
得分: 1
你可以使用数组的 every
和 some
方法来实现你的目标。请参考下面的代码片段。
let arr_c = arr_b.filter(a => arr_a.some(a1 => a1.id === a.id && a.type !== a1.type) || arr_a.every(a1 => a1.id !== a.id))
完整的可运行代码如下:
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-js -->
var arr_a = [{id:1, name:"thumbler", type:"add"},
{id:2, name:"spoon", type:"add"},
{id:3, name:"plate", type:"add"}];
var arr_b = [{id:1, name:"thumbler", type:"remove"},
{id:2, name:"spoon", type:"add"},
{id:3, name:"plate", type:"add"},
{id:4, name:"fork", type:"add"}];
var arr_c = arr_b.filter(a => arr_a.some(a1 => a1.id === a.id && a.type !== a1.type) || arr_a.every(a1 => a1.id !== a.id));
console.log(arr_c);
<!-- 结束代码片段 -->
英文:
You can use every
and some
methods of the array to achieve your result. See the snippet below.
let arr_c = arr_b.filter(a => arr_a.some(a1 => a1.id === a.id && a.type !== a1.type) || arr_a.every(a1 => a1.id !== a.id))
Full working code:-
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var arr_a = [{id:1, name:"thumbler", type:"add"},
{id:2, name:"spoon", type:"add"},
{id:3, name:"plate", type:"add"}];
var arr_b = [{id:1, name:"thumbler", type:"remove"},
{id:2, name:"spoon", type:"add"},
{id:3, name:"plate", type:"add"},
{id:4, name:"fork", type:"add"}];
var arr_c = arr_b.filter(a => arr_a.some(a1 => a1.id === a.id && a.type !== a1.type) || arr_a.every(a1 => a1.id !== a.id));
console.log(arr_c);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论