簡體   English   中英

如何在身份上比較對象的兩個大數組

[英]How to compare two big arrays of objects on identity

我必須在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有一些方法可以簡化任務,我不知道。 任何答案都將被考慮。 提前致謝。

是的,lodash具有使生活更輕松的方法_.isEqual()

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

或者,使用JSON方法:

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

您可以對數組進行字符串化並比較字符串

 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); 

如果我理解您的問題,您正在嘗試查看兩個對象數組是否相等。 在香草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 

數組是引用類型,因此比較它們只會比較它們的引用而不是它們的內容。

比較數組內容的一種快速方法(編程方式)是使用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)); 

有幾種選擇,在這里您幾乎沒有選擇,但是如果您想使用純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(&&)使用過濾器就可以編寫多個條件。 filter是JS ES6方法,適合解決此類問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM