如何在身份上比较对象的两个大数组

时间:2019-02-28 11:44:11

标签: javascript arrays reactjs object lodash

我必须在react componentwillreceiveprops中比较两个大对象数组。我拥有261个国家。例如,它是从此开始的。

 countries = [
     {id: 1, name: "Australia", code: "AU", isRemoved: false}
      ...
      261 objects
 ]

我必须比较下一个道具将接收的数组与我当前的国家/地区数组绝对相同,即所有属性,长度,属性值,所有条件均相等。请帮我。我写了这样的东西,但我只知道==不正确。

if (this.state.countriesInitial !== countries) {//TODO: compare arrays
      this.setState({
          countriesInitial: countries
      })
}

任何答案都将计入帐户。也许lodash有一些方法可以简化任务,我不知道。任何答案都将被考虑。预先感谢。

5 个答案:

答案 0 :(得分:0)

您可以对数组进行字符串化处理并比较字符串

const arr1 = [
  {
    id:1,
    name: 'Australia'
  },
  {
    id:2,
    name: 'Greece'
  }
];

const arr2 = [
  {
    id:1,
    name: 'Australia'
  },
  {
    id:2,
    name: 'Greece'
  }
];

function arraysEqual(arr1, arr2) {
  return JSON.stringify(arr1) === JSON.stringify(arr2);
}

const compare = arraysEqual(arr1, arr2);
console.log(compare);

答案 1 :(得分:0)

是的,lodash有一种使生活更轻松的方法-_.isEqual()`

<L>

或者,使用JSON方法:

var arr1 = [ /* Very long array */ ],
    arr2 = [ /* Also long array */ ];
var equalArrays = _.isEqual(arr1, arr2);

答案 2 :(得分:0)

如果我理解您的问题,那么您正在尝试查看两个对象数组是否彼此相等。在香草JS中可以做到这一点的一种方法是使用JSON.stringify()

const countries = [{id: 1, name: "Australia", code: "AU", isRemoved: false}],
nextProp = [{id: 1, name: "Australia", code: "AU", isRemoved: false}],

checkEq = (arr, arr2) =>
    JSON.stringify(arr) === JSON.stringify(arr2)

console.log(checkEq(countries, nextProp)); // true
console.log(checkEq([{a: 1}], [{a: 1}, {b: 2}])); // false

答案 3 :(得分:0)

  

数组是引用类型,因此比较它们只会比较它们的引用而不是它们的内容。

(编程方式)的一种快速方法是将数组用JSON.stringify()进行字符串化并比较结果字符串:

const arr1 = [
  {id: 1, name: "Australia", code: "AU", isRemoved: false},
  {id: 2, name: "France", code: "FR", isRemoved: false}
];
const arr2 = [
  {id: 1, name: "Australia", code: "AU", isRemoved: false},
  {id: 2, name: "France", code: "FR", isRemoved: false}
];
const arr3 = [
  {id: 1, name: "Austria", code: "AS", isRemoved: false},
  {id: 2, name: "France", code: "FR", isRemoved: false}
];

console.log(JSON.stringify(arr1) === JSON.stringify(arr2));
console.log(JSON.stringify(arr1) === JSON.stringify(arr3));

或者是比对整个数组进行字符串化处理更快速的方法,首先是比较长度,如果匹配,则使用Object.entries()Array.every()比较每个条目。

如果您输入的字段是原始类型(不是数组或对象),这将起作用,否则,您将需要以下内容的递归版本:

const sameEntries = (x, y) => {
  const xEntries = Object.entries(x);
  if (xEntries.length !== Object.entries(y).length) return false;
  return xEntries.every(([k,v]) => y[k] === v);
}

const sameArrays = (arr1, arr2) =>
  arr1.length === arr2.length && arr1.every((x, i) => sameEntries(x, arr2[i]));

const arr1 = [
  {id: 1, name: "Australia", code: "AU", isRemoved: false},
  {id: 2, name: "France", code: "FR", isRemoved: false}
];
const arr2 = [
  {id: 1, name: "Australia", code: "AU", isRemoved: false},
  {id: 2, name: "France", code: "FR", isRemoved: false}
];
const arr3 = [
  {id: 1, name: "Austria", code: "AS", isRemoved: false},
  {id: 2, name: "France", code: "FR", isRemoved: false}
];

console.log(sameArrays(arr1, arr2));
console.log(sameArrays(arr1, arr3));

答案 4 :(得分:0)

有几个选项,您在这里几乎没有,但是如果您想使用纯JS来做,您可以使用过滤器,我在这里给您举个例子,也许有帮助。

我们有一个名为Companies的数组:

const companies = [
    {name: "blueweb360", category: "internet", start: 2013, end: 2014},
    {name: "Himo", category: "internet", start: 2015, end: 2016},
    {name: "blue", category: "IOT", start: 2014, end: 2017},
    {name: "waac", category: "Tech", start: 2016, end: 2019}

];

现在我们需要拥有类别等于互联网的公司:

const retail = companies.filter(iterator => iterator.category == "internet")

document.write(JSON.stringify(retail)) //or you can write console.log(retail)

仅使用or(||)和and(&&)即可使用过滤器,您可以编写多个条件。 过滤器是JS ES6方法,适合解决此类问题。