简体   繁体   English

比较两个 Arrays 对象,如果 Javascript 中的值为真,则返回一个新数组

[英]Compare Two Arrays Of Objects and return a new array if value is true in Javascript

I need to compare 2 arrays我需要比较 2 arrays

const inviteFriends = [
  {
    userId: 'u12p3',
    name: 'Goku',
    invited: true
  },
  {
    userId: 'uefi3',
    name: 'Vegeta',
    invited: true
  }
]

const allFriends = [
  {
    userId: 'u12p3',
    name: 'Goku',
    invited: false
  },
  {
    userId: 'ufisj',
    name: 'Goten',
    invited: false
  },
  {
    userId: 'uefi3',
    name: 'Vegeta',
    invited: false
  },
]

An if invited is true I need to return a new array.如果invitedtrue ,我需要返回一个新数组。

Something like this:像这样的东西:

const newArray = [
  {
    userId: 'u12p3',
    name: 'Goku',
    invited: true
  },
  {
    userId: 'ufisj',
    name: 'Goten',
    invited: false
  },
  {
    userId: 'uefi3',
    name: 'Vegeta',
    invited: true
  },
]

Any idea how can I achieve this?知道如何实现这一目标吗? Help please请帮忙

 const inviteFriends = [ { userId: 'u12p3', name: 'Goku', invited: true }, { userId: 'uefi3', name: 'Vegeta', invited: true } ] const allFriends = [ { userId: 'u12p3', name: 'Goku', invited: false }, { userId: 'ufisj', name: 'Goten', invited: false }, { userId: 'uefi3', name: 'Vegeta', invited: false }, ]; const newArr = allFriends.map((friend) => { const found = inviteFriends.find((invited) => { return invited.userId === friend.userId }); return {...friend, ...found}; }); console.log(newArr);

Use filtering to achieve that:使用过滤来实现:

invitedFriends = allFriends.filter(friend => friend.invited==true);

since you also need the,invited row, so the logic is to replace the same data from invitedFriends to allFriends (compare by id)因为您还需要,invited 行,所以逻辑是将相同的数据从受邀朋友替换为 allFriends(按 id 比较)

we loop on the allfriends then find if allfriend(n).id found on invitedFriend if found then push invitedFriend(found) if not just push the allfriend(n)我们在 allfriends 上循环,然后查找 allfriend(n).id 是否在受邀朋友上找到,如果找到,然后推送受邀朋友(found),如果不只是推送 allfriend(n)

so in one line you can use所以在一行中你可以使用

let x = allFriends.map( af =>  inviteFriends.find( x => x.userId === af.userId) || af );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较 JavaScript 中的两个 arrays 对象,如果为真,则将新项目保存到 MongoDB - compare two arrays of Objects in JavaScript and if true save new items to MongoDB 比较两个对象数组,如果匹配则返回 true - Compare two array of objects and return true if they are match 比较对象的 arrays 并返回不在 arrays 之一中的新对象数组 - Compare arrays of objects and return new array of objects that are not in one of the arrays 如何深入比较两个javascript对象并返回所有差异,包括新添加的数组和相同的原始格式? - How to deep compare two javascript objects and return all the differences including new added arrays and with the same original format? 如何比较两个 arrays 并且只返回正确的值 - How can I compare two arrays and only return value that are true javascript:通过某个 id 比较两个对象数组的内容并返回 true 或 false - javascript : compare content of two array of objects by some id and return true or false 将两个Arrays与Objects进行比较,并使用不匹配的对象创建新数组 - Compare two Arrays with Objects and create new array with unmatched objects 比较两个数组中的对象并根据javascript中的匹配返回 - Compare objects in two arrays and return based on match in javascript 比较 Javascript 中对象的两个 arrays - Compare two arrays of objects in Javascript 如何比较两个 arrays 并在 js 中返回 true - how to compare two arrays and return true in js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM