如何过滤对象数组与另一个对象数组并获得差异

时间:2019-08-22 15:19:48

标签: javascript arrays object

我担心如何用另一个对象数组过滤对象数组并获得第一个对象数组上没有的项目

例如:

let firstData = [
    {
        name: 'A',
        title: 'A'
    },
    {
        name: 'B',
        title: 'B'
    },
    {
        name: 'C',
        title: 'C'
    }
]


let secondData = [
    {
        name: 'B',
        title: 'B'
    }
]

在这种情况下,我想获取每个对象数组中都没有的项:A和名称:C

例如,如何将它们与名称属性进行比较?

5 个答案:

答案 0 :(得分:1)

过滤第一个数组,并检查第二个数组的some是否具有具有相同的name属性的对象:

let firstData = [
  {
    name: "A",
    title: "A"
  },
  {
    name: "B",
    title: "B"
  },
  {
    name: "C",
    title: "C"
  }
];

let secondData = [
  {
    name: "B",
    title: "B"
  }
];

const result = firstData.filter(
  ({ name }) => secondData.some(({ name: secondName }) => secondName !== name)
);

console.log(result);

答案 1 :(得分:1)

尝试更通用的方法:没有secondData的firstData(左)加上没有firstData的secondData(右)

let left  = firstData.filter(x=> !secondData.find(y=> x.name==y.name));
let right = secondData.filter(x=> !firstData.find(y=> x.name==y.name));
let result = left.concat(right);

let firstData = [
    {
        name: 'A',
        title: 'A'
    },
    {
        name: 'B',
        title: 'B'
    },
    {
        name: 'C',
        title: 'C'
    }
]


let secondData = [
    {
        name: 'B',
        title: 'B'
    },
    
    {
        name: 'D',
        title: 'D'
    }
]

let left  = firstData.filter(x=> !secondData.find(y=> x.name==y.name));
let right = secondData.filter(x=> !firstData.find(y=> x.name==y.name));
let result = left.concat(right);

console.log(result);

答案 2 :(得分:1)

如何?

const filteredElements = firstData.reduce((acc, firstElement) => {
    for(let secondElement of secondData) {

        for(let firstProperty in firstElement) // Does the first element have a value that doesn't exist in the second element?
            if(!secondElement.hasOwnProperty(firstProperty) || secondElement[firstProperty] !== firstElement[firstProperty])
                return acc.concat(firstElement)

        for(let secondProperty in secondElement) // Does the second element have a value that doesn't exist in the first element?
            if(!firstElement.hasOwnProperty(secondProperty) || firstElement[secondProperty] !== secondElement[secondProperty])
                return acc.concat(firstElement)

        return acc // No differing values means this element exists in the second object, so we ignore it       
    }


    return acc.concat(firstElement)
}, [])

console.log(filteredElements) // A and C

编辑:这将比较两个对象中的所有值并寻找差异。如果您只想比较特定的值,则可以将firstPropertysecondProperty设置为所需字段的名称。

答案 3 :(得分:0)

使用lodash是最简单的存档方式:

_.differenceWith(firstData,secondData, (left, right) => left.name === right.name)

let firstData = [
  {
    name: "A",
    title: "A"
  },
  {
    name: "B",
    title: "B"
  },
  {
    name: "C",
    title: "C"
  }
];

let secondData = [
  {
    name: "B",
    title: "B"
  }
];
console.log(
  _.differenceWith(firstData,secondData, (left, right) => left.name === right.name)
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

答案 4 :(得分:0)

两个数组比较

  

将两个数组与名称和标题之类的所有数据进行比较

585abf5d005a8eb3c2d6e2851988cc0c,586556570100dac0eb73fcb231007126
585ac054005b72f16d9c6742858707c8,588b9329004c4d7a5e27eb75b43c4c95
585ac054005b73207719baeda7b97779,588be66300aba64caf9818cb4e194413

Source Code

    let firstData = [
    {
        name: 'A',
        title: 'A'
    },
    {
        name: 'B',
        title: 'F'
    },
    {
        name: 'C',
        title: 'C'
    }
];

let secondData = [
    {
        name: 'B',
        title: 'B'
    },
    {
        name: 'C',
        title: 'C'
    },
    {
        name: 'D',
        title: 'D'
    }
];

var First = firstData.filter(item => !secondData.find(e => item.name == e.name && item.title == e.title));

var Second  = secondData.filter(item => !firstData.find(e => item.name == e.name && item.title == e.title));

let result = First.concat(Second);

console.log(result);

//output

// Array(5)
// 0: {name: "A", title: "A"}
// 1: {name: "B", title: "F"}
// 2: {name: "B", title: "B"}
// 3: {name: "D", title: "D"}
// length: 4