React JS比较两个不同对象数组中的值。

huangapple go评论76阅读模式
英文:

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

你可以使用数组的 everysome 方法来实现你的目标。请参考下面的代码片段。

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 =&gt; arr_a.some(a1 =&gt; a1.id === a.id &amp;&amp; a.type !== a1.type) || arr_a.every(a1 =&gt; 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:&quot;thumbler&quot;, type:&quot;add&quot;},
             {id:2, name:&quot;spoon&quot;, type:&quot;add&quot;},
             {id:3, name:&quot;plate&quot;, type:&quot;add&quot;}];

var arr_b = [{id:1, name:&quot;thumbler&quot;, type:&quot;remove&quot;},
             {id:2, name:&quot;spoon&quot;, type:&quot;add&quot;},
             {id:3, name:&quot;plate&quot;, type:&quot;add&quot;},
             {id:4, name:&quot;fork&quot;, type:&quot;add&quot;}];
var arr_c = arr_b.filter(a =&gt; arr_a.some(a1 =&gt; a1.id === a.id &amp;&amp; a.type !== a1.type) || arr_a.every(a1 =&gt; a1.id !== a.id));

console.log(arr_c);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月26日 13:30:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定