NodeJS 比较两个对象数组,第二个数组中不存在筛选器元素

fumotvh3  于 2023-05-28  发布在  Node.js
关注(0)|答案(4)|浏览(209)

假设我有两个对象数组:

const array1 = [
  { name: 'detail1', title: 'detail1' },
  { name: 'detail2 ', title: 'detail2 ' },
  { name: 'detail3', title: 'detail3' },
  { name: 'detail4', title: 'detail4' },
  { name: 'detail5', title: 'detail5' },
  { name: 'detail6', title: 'detail6' },
  { name: 'detail7', title: 'detail7' }
]

const array2 = [
  { name: 'detail1', title: 'detail1' },
  { name: 'detail2 ', title: 'detail2 ' },
  { name: 'detail3', title: 'detail3' },
  { name: 'detail4', title: 'detail4' },
]

我想比较两个数组,即array 1和array 2,得到array 2中缺少的元素。
为此,我尝试了:

var absent = array2.filter(e=>!array1.includes(e));

但是我无法得到array 2的缺失元素。
我期待的O/P:

[ { name: 'detail5', title: 'detail5' },
            { name: 'detail6', title: 'detail6' },
            { name: 'detail7', title: 'detail7' }]

这些是所有不在array 2中的元素。
我到底做错了什么?
如果有人需要进一步的信息,请告诉我。

cetgtptt

cetgtptt1#

你可以用键和值构建一个标准化的对象,然后过滤对象。

const
    array1 = [{ name: 'detail1', title: 'detail1' }, { name: 'detail2 ', title: 'detail2 ' }, { name: 'detail3', title: 'detail3' }, { name: 'detail4', title: 'detail4' }, { name: 'detail5', title: 'detail6' }, { name: 'detail7', title: 'detail7' }, { name: 'detail8', title: 'detail8' }],
    array2 = [{ name: 'detail1', title: 'detail1' }, { name: 'detail2 ', title: 'detail2 ' }, { name: 'detail3', title: 'detail3' }, { name: 'detail4', title: 'detail4' }],
    sortEntriesByKey = ([a], [b]) => a.localeCompare(b),
    filter = array2.reduce((r, o) => {
        Object
            .entries(o)
            .sort(sortEntriesByKey)
            .reduce((o, [k, v]) => (o[k] ??= {})[v] ??= {}, r);
        return r;
    }, {});
    absent = array1.filter((o) => {
        let f = filter;
        return !Object
            .entries(o)
            .sort(sortEntriesByKey)
            .every(([k, v]) => f = f[k]?.[v]);
    });

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

nfzehxib2#

编辑:你想要的对象在A而不是在B。理想的做法是遍历A,找出元素是否存在于B中。如果是,则不包括它。
在javacript中,当你使用“==”或“===”或其他数组搜索方法时,对象引用会被比较。
{} == {}将返回false。您可以在您的开发控制台中进行检查。
在这种情况下,您必须检查特定的属性。

var absent = array1.filter(e=>{
  let findInd = array2.findIndex((a)=>{
    return (a.name == e.name && a.title == e.title);});
return (findInd == -1); });

在内部的findIndex中,我根据一个条件查找索引。在filter方法中,只有当索引为-1(未找到)时才返回true。

exdqitrt

exdqitrt3#

这对我很有效:

let cart = [{
    "id": "6304a51af5726921c0dadd64",
    "qty": 1
  },
  {
    "id": "8704a51af5726921c0dadd64",
    "qty": 1
  },
  {
    "id": "4704a51af5726921c0dadd64",
    "qty": 1
  }
]

let cartList = [{
    "id": "6304a51af5726921c0dadd64",
    "qty": 1
  },
  {
    "id": "9704a51af5726921c0dadd64",
    "qty": 1
  }
]

let test = cart.some((element) =>
  cartList.some((e) => element.id === e.id)
);
console.log(" if any single object matched:", test);

let test1 = cart.filter((element) =>
  cartList.some((e) => element.id === e.id)
);
console.log("display matching objects :", test1);
mspsb9vt

mspsb9vt4#

const array1 = [
  { name: 'detail1', title: 'detail1' },
  { name: 'detail2 ', title: 'detail2 ' },
  { name: 'detail3', title: 'detail3' },
  { name: 'detail4', title: 'detail4' },
  { name: 'detail5', title: 'detail5' },
  { name: 'detail6', title: 'detail6' },
  { name: 'detail7', title: 'detail7' }
]

const array2 = [
  { name: 'detail1', title: 'detail1' },
  { name: 'detail2 ', title: 'detail2 ' },
  { name: 'detail3', title: 'detail3' },
  { name: 'detail4', title: 'detail4' },
]
const thirdArray = array1.filter((elem) => {
       return !array2.some((ele) => {
       return elem.name === ele.name
         });
       });

相关问题