比较 JavaScript 中的对象数组

Comparing Arrays of Objects in JavaScript

提问人:AdamB 提问时间:8/26/2008 最后编辑:dreftymacAdamB 更新时间:3/16/2023 访问量:396362

问:

我想比较 JavaScript 代码中的 2 个对象数组。这些对象总共有 8 个属性,但每个对象不会为每个对象设置一个值,并且每个数组永远不会大于 8 个项目,所以也许遍历每个属性然后查看 8 个属性值的蛮力方法是做我想做的事情的最简单方法, 但在实施之前,我想看看是否有人有更优雅的解决方案。有什么想法吗?

JavaScript 数组 比较 Array-of-dict

评论


答:

70赞 Jason Bunting 8/26/2008 #1

编辑:您不能在当前常见的基于浏览器的 JavaScript 解释器实现中重载运算符。

要回答最初的问题,您可以这样做的一种方法,请注意,这有点黑客,只需将两个数组序列化为 JSON,然后比较两个 JSON 字符串。这只会告诉你数组是否不同,显然你也可以对数组中的每个对象执行此操作,以查看哪些对象是不同的。

另一种选择是使用一个库,该库具有一些用于比较对象的良好工具 - 我使用并推荐 MochiKit


编辑:kamens给出的答案也值得考虑,因为比较两个给定对象的单个函数将比任何库小得多,以完成我的建议(尽管我的建议肯定足够好用)。

这是一个幼稚的实现,可能对你来说已经足够了 - 请注意,这个实现存在潜在的问题:

function objectsAreSame(x, y) {
   var objectsAreSame = true;
   for(var propertyName in x) {
      if(x[propertyName] !== y[propertyName]) {
         objectsAreSame = false;
         break;
      }
   }
   return objectsAreSame;
}

假设这两个对象具有完全相同的属性列表。

哦,很明显,无论好坏,我都属于唯一的一个回归点阵营。:)

评论

2赞 Alan H. 2/12/2011
只是为了指出限制:在比较包含对象的对象时,这看起来会失败。(正如你所提到的,当两个对象没有“完全相同的属性列表”时,它将失败,因为允许它是 的超集合。yx
4赞 Alan H. 2/12/2011
关于 JSON 序列化建议的一个警告是,如果您正在比较对象(而不是数组)并且不关心顺序(例如命名键,而不是数字数组),那么 JSON 序列化将不起作用。
0赞 zyxue 4/22/2017
@AlanH。您的意思是应该用于比较两个非对象(例如数字、字符串)数组以及两个对象数组,但不适用于比较两个对象?如果是这样,为什么?特别是在比较两个 Object 数组的情况下?JSON.stringify
5赞 Alan H. 4/23/2017
忘记对象数组和对象数组,无论这意味着什么。想想看和.我们不关心对象的顺序,但这些字符串显然是不同的。{a: 1, b: 1}{b: 1, a: 1}
23赞 kamens 8/26/2008 #2

老实说,如果每个对象最多有 8 个对象,最多有 8 个属性,最好的办法是遍历每个对象并直接进行比较。它会很快,而且很容易。

如果您要经常使用这些类型的比较,那么我同意 Jason 关于 JSON 序列化的观点......但除此之外,无需使用新库或 JSON 序列化代码来减慢应用的速度。

1赞 utcursch 3/16/2009 #3

@JasonBunting 的回答中提到的功能对我来说效果很好。但是,有一个小问题:如果 和 是对象 (),则需要递归调用该函数才能比较它们。objectsAreSamex[propertyName]y[propertyName]typeof x[propertyName] == 'object'

18赞 Yuval 9/20/2010 #4

我研究了一个简单的算法来比较两个对象的内容并返回一个可理解的差异列表。以为我会分享。它借鉴了jQuery的一些思想,即函数实现以及对象和数组类型检查。map

它返回一个“diff 对象”列表,这些对象是带有 diff 信息的数组。这很简单。

在这里:

// compare contents of two objects and return a list of differences
// returns an array where each element is also an array in the form:
// [accessor, diffType, leftValue, rightValue ]
//
// diffType is one of the following:
//   value: when primitive values at that index are different
//   undefined: when values in that index exist in one object but don't in 
//              another; one of the values is always undefined
//   null: when a value in that index is null or undefined; values are
//         expressed as boolean values, indicated wheter they were nulls
//   type: when values in that index are of different types; values are 
//         expressed as types
//   length: when arrays in that index are of different length; values are
//           the lengths of the arrays
//

function DiffObjects(o1, o2) {
    // choose a map() impl.
    // you may use $.map from jQuery if you wish
    var map = Array.prototype.map?
        function(a) { return Array.prototype.map.apply(a, Array.prototype.slice.call(arguments, 1)); } :
        function(a, f) { 
            var ret = new Array(a.length), value;
            for ( var i = 0, length = a.length; i < length; i++ ) 
                ret[i] = f(a[i], i);
            return ret.concat();
        };

    // shorthand for push impl.
    var push = Array.prototype.push;

    // check for null/undefined values
    if ((o1 == null) || (o2 == null)) {
        if (o1 != o2)
            return [["", "null", o1!=null, o2!=null]];

        return undefined; // both null
    }
    // compare types
    if ((o1.constructor != o2.constructor) ||
        (typeof o1 != typeof o2)) {
        return [["", "type", Object.prototype.toString.call(o1), Object.prototype.toString.call(o2) ]]; // different type

    }

    // compare arrays
    if (Object.prototype.toString.call(o1) == "[object Array]") {
        if (o1.length != o2.length) { 
            return [["", "length", o1.length, o2.length]]; // different length
        }
        var diff =[];
        for (var i=0; i<o1.length; i++) {
            // per element nested diff
            var innerDiff = DiffObjects(o1[i], o2[i]);
            if (innerDiff) { // o1[i] != o2[i]
                // merge diff array into parent's while including parent object name ([i])
                push.apply(diff, map(innerDiff, function(o, j) { o[0]="[" + i + "]" + o[0]; return o; }));
            }
        }
        // if any differences were found, return them
        if (diff.length)
            return diff;
        // return nothing if arrays equal
        return undefined;
    }

    // compare object trees
    if (Object.prototype.toString.call(o1) == "[object Object]") {
        var diff =[];
        // check all props in o1
        for (var prop in o1) {
            // the double check in o1 is because in V8 objects remember keys set to undefined 
            if ((typeof o2[prop] == "undefined") && (typeof o1[prop] != "undefined")) {
                // prop exists in o1 but not in o2
                diff.push(["[" + prop + "]", "undefined", o1[prop], undefined]); // prop exists in o1 but not in o2

            }
            else {
                // per element nested diff
                var innerDiff = DiffObjects(o1[prop], o2[prop]);
                if (innerDiff) { // o1[prop] != o2[prop]
                    // merge diff array into parent's while including parent object name ([prop])
                    push.apply(diff, map(innerDiff, function(o, j) { o[0]="[" + prop + "]" + o[0]; return o; }));
                }

            }
        }
        for (var prop in o2) {
            // the double check in o2 is because in V8 objects remember keys set to undefined 
            if ((typeof o1[prop] == "undefined") && (typeof o2[prop] != "undefined")) {
                // prop exists in o2 but not in o1
                diff.push(["[" + prop + "]", "undefined", undefined, o2[prop]]); // prop exists in o2 but not in o1

            }
        }
        // if any differences were found, return them
        if (diff.length)
            return diff;
        // return nothing if objects equal
        return undefined;
    }
    // if same type and not null or objects or arrays
    // perform primitive value comparison
    if (o1 != o2)
        return [["", "value", o1, o2]];

    // return nothing if values are equal
    return undefined;
}
20赞 jwood 5/20/2011 #5

I know this is an old question and the answers provided work fine ... but this is a bit shorter and doesn't require any additional libraries ( i.e. JSON ):

function arraysAreEqual(ary1,ary2){
  return (ary1.join('') == ary2.join(''));
}

评论

18赞 Jonathan M 10/8/2011
The OP wanted to join arrays of objects. This only works for arrays of scalars.
12赞 Jason Moore 3/1/2012
It's also fragile. If: then a on the two different arrays will result in a=["1,2"] , b=["1", "2"]join()'1,2'
0赞 Ernest 7/24/2012
@Jason Moore not true, a.join('') // => "1,2"; b.join('') // => "12"
9赞 Qsario 8/1/2012
You are correct about that particular example, but it's still fragile. leads to and I don't think any delimiter can save you because it could be in the obj itself. Nor can a length check fix it, because a=["12"], b=["1", "2"]"12"=="12"a=["12", "3"], b=["1", "23"]
5赞 Brice Roncace 12/21/2014
A slightly more robust implementation: return ary1.join(',') === ary2.join(',');
1赞 Keshav Kalra 8/11/2014 #6

Please try this one:

function used_to_compare_two_arrays(a, b)
{
  // This block will make the array of indexed that array b contains a elements
  var c = a.filter(function(value, index, obj) {
    return b.indexOf(value) > -1;
  });

  // This is used for making comparison that both have same length if no condition go wrong 
  if (c.length !== a.length) {
    return 0;
  } else{
    return 1;
  }
}

评论

0赞 Christian 12/31/2020
uhm... the if statement can be simplified to just return c.length === a.length;
3赞 Maxime Pacary 3/20/2017 #7

Here is my attempt, using Node's assert module + npm package object-hash.

I suppose that you would like to check if two arrays contain the same objects, even if those objects are ordered differently between the two arrays.

var assert = require('assert');
var hash = require('object-hash');

var obj1 = {a: 1, b: 2, c: 333},
    obj2 = {b: 2, a: 1, c: 444},
    obj3 = {b: "AAA", c: 555},
    obj4 = {c: 555, b: "AAA"};

var array1 = [obj1, obj2, obj3, obj4];
var array2 = [obj3, obj2, obj4, obj1]; // [obj3, obj3, obj2, obj1] should work as well

// calling assert.deepEquals(array1, array2) at this point FAILS (throws an AssertionError)
// even if array1 and array2 contain the same objects in different order,
// because array1[0].c !== array2[0].c

// sort objects in arrays by their hashes, so that if the arrays are identical,
// their objects can be compared in the same order, one by one
var array1 = sortArrayOnHash(array1);
var array2 = sortArrayOnHash(array2);

// then, this should output "PASS"
try {
    assert.deepEqual(array1, array2);
    console.log("PASS");
} catch (e) {
    console.log("FAIL");
    console.log(e);
}

// You could define as well something like Array.prototype.sortOnHash()...
function sortArrayOnHash(array) {
    return array.sort(function(a, b) {
        return hash(a) > hash(b);
    });
}
17赞 Sanjay Verma 10/17/2018 #8

I tried and worked for me.JSON.stringify()

let array1 = [1,2,{value:'alpha'}] , array2 = [{value:'alpha'},'music',3,4];

JSON.stringify(array1) // "[1,2,{"value":"alpha"}]"

JSON.stringify(array2) // "[{"value":"alpha"},"music",3,4]"

JSON.stringify(array1) === JSON.stringify(array2); // false

评论

29赞 ganeshk 1/8/2019
To note here - this will not work if the object properties are out of order
0赞 TheEhsanSarshar 11/8/2020
first we can sort the array and then use stringify
3赞 Christian 12/31/2020
@Ehsansarshar that won't work... you need to sort all object properties and arrays...
-2赞 Henry Sellars 1/4/2019 #9

using from lodash: https://lodash.com/docs/4.17.11#some_.some

const array1AndArray2NotEqual = 
          _.some(array1, (a1, idx) => a1.key1 !== array2[idx].key1 
                                     || a1.key2 !== array2[idx].key2 
                                     || a1.key3 !== array2[idx].key3);
109赞 ttulka 3/20/2019 #10

As serialization doesn't work generally (only when the order of properties matches: ) you have to check the count of properties and compare each property as well:JSON.stringify({a:1,b:2}) !== JSON.stringify({b:2,a:1})

const objectsEqual = (o1, o2) =>
    Object.keys(o1).length === Object.keys(o2).length 
        && Object.keys(o1).every(p => o1[p] === o2[p]);

const obj1 = { name: 'John', age: 33};
const obj2 = { age: 33, name: 'John' };
const obj3 = { name: 'John', age: 45 };
        
console.log(objectsEqual(obj1, obj2)); // true
console.log(objectsEqual(obj1, obj3)); // false

If you need a deep comparison, you can call the function recursively:

const obj1 = { name: 'John', age: 33, info: { married: true, hobbies: ['sport', 'art'] } };
const obj2 = { age: 33, name: 'John', info: { hobbies: ['sport', 'art'], married: true } };
const obj3 = { name: 'John', age: 33 };

const objectsEqual = (o1, o2) => 
    typeof o1 === 'object' && Object.keys(o1).length > 0 
        ? Object.keys(o1).length === Object.keys(o2).length 
            && Object.keys(o1).every(p => objectsEqual(o1[p], o2[p]))
        : o1 === o2;
        
console.log(objectsEqual(obj1, obj2)); // true
console.log(objectsEqual(obj1, obj3)); // false

Then it's easy to use this function to compare objects in arrays:

const arr1 = [obj1, obj1];
const arr2 = [obj1, obj2];
const arr3 = [obj1, obj3];

const arraysEqual = (a1, a2) => 
   a1.length === a2.length && a1.every((o, idx) => objectsEqual(o, a2[idx]));

console.log(arraysEqual(arr1, arr2)); // true
console.log(arraysEqual(arr1, arr3)); // false

评论

0赞 Socrates Tuas 8/3/2019
Great. Just wrap the deep comparison with validation to work with null property or else return . Cool.o1 === o2
5赞 srikanth ramesh 3/25/2021
doesn't take order into consideration. Won't work for below scenario const arr1 = [obj1, obj2]; const arr2 = [obj2, obj1];
2赞 Saurabh 8/31/2021
Best answer and saved tons of time for me. Thank you!
1赞 Cels 11/1/2021
fails for [1,2] and [2,1]
1赞 ttulka 11/2/2021
@Cels It's correct behavior, [1,2] and [2,1] are not equal.
-1赞 Flash Noob 1/30/2021 #11

comparing with json is pretty bad. try this package to compare nested arrays and get the difference.

https://www.npmjs.com/package/deep-object-diff

评论

0赞 Heretic Monkey 8/10/2022
This is a link to an answer. It would be better if it showed how to use the library linked to to solve the problem the OP presents.
7赞 Wolfram 2/11/2021 #12

There is a optimized code for case when function needs to equals to empty arrays (and returning false in that case)

const objectsEqual = (o1, o2) => {
    if (o2 === null && o1 !== null) return false;
    return o1 !== null && typeof o1 === 'object' && Object.keys(o1).length > 0 ?
        Object.keys(o1).length === Object.keys(o2).length && 
        Object.keys(o1).every(p => objectsEqual(o1[p], o2[p]))
        : (o1 !== null && Array.isArray(o1) && Array.isArray(o2) && !o1.length && 
        !o2.length) ? true : o1 === o2;
}

评论

0赞 OMANSAK 8/12/2021
this is fast solution ++rep
0赞 captainblack 11/1/2022
Best solution ever, congrulations it is working truely
0赞 Vladimir Malikov 2/15/2021 #13

There`s my solution. It will compare arrays which also have objects and arrays. Elements can be stay in any positions. Example:

const array1 = [{a: 1}, {b: 2}, { c: 0, d: { e: 1, f: 2, } }, [1,2,3,54]];
const array2 = [{a: 1}, {b: 2}, { c: 0, d: { e: 1, f: 2, } }, [1,2,3,54]];

const arraysCompare = (a1, a2) => {
  if (a1.length !== a2.length) return false;
  const objectIteration = (object) => {
    const result = [];
    const objectReduce = (obj) => {
      for (let i in obj) {
        if (typeof obj[i] !== 'object') {
          result.push(`${i}${obj[i]}`);
        } else {
          objectReduce(obj[i]);
        }
      }
    };
    objectReduce(object);
    return result;
  };
  const reduceArray1 = a1.map(item => {
    if (typeof item !== 'object') return item;
    return objectIteration(item).join('');
  });
  const reduceArray2 = a2.map(item => {
    if (typeof item !== 'object') return item;
    return objectIteration(item).join('');
  });
  const compare =  reduceArray1.map(item => reduceArray2.includes(item));
  return compare.reduce((acc, item) => acc + Number(item)) === a1.length;
};

console.log(arraysCompare(array1, array2));
3赞 Shawn W 5/11/2021 #14

My practice implementation with sorting, tested and working.

const obj1 = { name: 'John', age: 33};
const obj2 = { age: 33, name: 'John' };
const obj3 = { name: 'John', age: 45 };

const equalObjs = ( obj1, obj2 ) => {
let keyExist = false;
for ( const [key, value] of Object.entries(obj1) ) {
     // Search each key in reference object and attach a callback function to 
     // compare the two object keys
    if( Object.keys(obj2).some( ( e ) => e == key ) ) {
        keyExist = true;
    }
}

return keyExist;

}


console.info( equalObjs( obj1, obj2 ) );

Compare your arrays

// Sort Arrays
    var arr1 = arr1.sort(( a, b ) => {
    var fa = Object.keys(a);
    var fb = Object.keys(b);

    if (fa < fb) {
        return -1;
    }
    if (fa > fb) {
        return 1;
    }
    return 0;
});

var arr2 = arr2.sort(( a, b ) => {
    var fa = Object.keys(a);
    var fb = Object.keys(b);

    if (fa < fb) {
        return -1;
    }
    if (fa > fb) {
        return 1;
    }
    return 0;
});

const equalArrays = ( arr1, arr2 ) => {
    // If the arrays are different length we an eliminate immediately
    if( arr1.length !== arr2.length ) {
        return false;
    } else if ( arr1.every(( obj, index ) => equalObjs( obj, arr2[index] ) ) ) {
        return true;
      } else { 
        return false;
      }
    }

    console.info( equalArrays( arr1, arr2 ) );

评论

0赞 captainblack 11/1/2022
This returns true when array diffrent .Example const obj1 = { name: 'Johnx', age: 33 } arr1.push(obj1) const obj2 = { age: 33, name: 'John' } arr2.push(obj2)
2赞 i6f70 11/9/2021 #15

I am sharing my compare function implementation as it might be helpful for others:

 /*
  null AND null // true
  undefined AND undefined // true
  null AND undefined // false
  [] AND [] // true
  [1, 2, 'test'] AND ['test', 2, 1] // true
  [1, 2, 'test'] AND ['test', 2, 3] // false
  [undefined, 2, 'test'] AND ['test', 2, 1] // false
  [undefined, 2, 'test'] AND ['test', 2, undefined] // true
  [[1, 2], 'test'] AND ['test', [2, 1]] // true
  [1, 'test'] AND ['test', [2, 1]] // false
  [[2, 1], 'test'] AND ['test', [2, 1]] // true
  [[2, 1], 'test'] AND ['test', [2, 3]] // false
  [[[3, 4], 2], 'test'] AND ['test', [2, [3, 4]]] // true
  [[[3, 4], 2], 'test'] AND ['test', [2, [5, 4]]] // false
  [{x: 1, y: 2}, 'test'] AND ['test', {x: 1, y: 2}] // true
  1 AND 1 // true
  {test: 1} AND ['test', 2, 1] // false
  {test: 1} AND {test: 1} // true
  {test: 1} AND {test: 2} // false
  {test: [1, 2]} AND {test: [1, 2]} // true
  {test: [1, 2]} AND {test: [1]} // false
  {test: [1, 2], x: 1} AND {test: [1, 2], x: 2} // false
  {test: [1, { z: 5 }], x: 1} AND {x: 1, test: [1, { z: 5}]} // true
  {test: [1, { z: 5 }], x: 1} AND {x: 1, test: [1, { z: 6}]} // false
   */
  function is_equal(x, y) {
      const
          arr1 = x,
          arr2 = y,
          is_objects_equal = function (obj_x, obj_y) {
              if (!(
                  typeof obj_x === 'object' &&
                  Object.keys(obj_x).length > 0
              ))
                  return obj_x === obj_y;

              return Object.keys(obj_x).length === Object.keys(obj_y).length &&
                  Object.keys(obj_x).every(p => is_objects_equal(obj_x[p], obj_y[p]));
          }
          ;

      if (!( Array.isArray(arr1) && Array.isArray(arr2) ))
          return (
              arr1 && typeof arr1 === 'object' &&
              arr2 && typeof arr2 === 'object'
          )
              ? is_objects_equal(arr1, arr2)
              : arr1 === arr2;

      if (arr1.length !== arr2.length)
          return false;

      for (const idx_1 of arr1.keys())
          for (const idx_2 of arr2.keys())
              if (
                  (
                      Array.isArray(arr1[idx_1]) &&
                      this.is_equal(arr1[idx_1], arr2[idx_2])
                  ) ||
                  is_objects_equal(arr1[idx_1], arr2[idx_2])
              )
              {
                  arr2.splice(idx_2, 1);
                  break;
              }

      return !arr2.length;
  }

2赞 Mr.P 12/9/2021 #16

不确定性能...将不得不在大物体上进行测试..但是,这对我来说很有效。与其他解决方案相比,它的优势在于,对象/数组不必处于相同的顺序......

它实际上获取第一个数组中的第一个对象,并扫描第二个数组以查找每个对象。如果是匹配项,则将继续进行另一个匹配项

绝对有一种优化方法,但它正在:)

感谢@ttulka我从他的作品中得到了启发......只是稍微做了一点

const objectsEqual = (o1, o2) => {
  let match = false
    if(typeof o1 === 'object' && Object.keys(o1).length > 0) {
     match = (Object.keys(o1).length === Object.keys(o2).length && Object.keys(o1).every(p => objectsEqual(o1[p], o2[p])))
    }else {
     match = (o1 === o2)
    }
    return match
}

const arraysEqual = (a1, a2) => {
  let finalMatch = []
  let itemFound = []
  
  if(a1.length === a2.length) {
    finalMatch = []
    a1.forEach( i1 => {
      itemFound = []
      a2.forEach( i2 => { 
        itemFound.push(objectsEqual(i1, i2)) 
      })
        finalMatch.push(itemFound.some( i => i === true))  
    }) 
  } 
  return finalMatch.every(i => i === true)
}

const ar1 = [
  { id: 1, name: "Johnny", data: { body: "Some text"}},
  { id: 2, name: "Jimmy"}
]
const ar2 = [
  {name: "Jimmy", id: 2},
  {name: "Johnny", data: { body: "Some text"}, id: 1}
]


console.log("Match:",arraysEqual(ar1, ar2))

jsfiddle:https://jsfiddle.net/x1pubs6q/

或者只使用 Lodash :))))

const _ = require('lodash')

const isArrayEqual = (x, y) => {
  return _.isEmpty(_.xorWith(x, y, _.isEqual));
};

评论

0赞 faint-hearted-fool 11/9/2022
太棒了,正是我想要的!
0赞 Oussama Filani 6/21/2022 #17

这是我在不考虑项目顺序的情况下比较两个对象数组的工作

const collection1 =  [
  { id: "1", name: "item 1", subtitle: "This is a subtitle", parentId: "1" },
  { id: "2", name: "item 2", parentId: "1" },
  { id: "3", name: "item 3", parentId: "1" },
]
const collection2 =  [
  { id: "3", name: "item 3", parentId: "1" },
  { id: "2", name: "item 2", parentId: "1" },
  { id: "1", name: "item 1", subtitle: "This is a subtitle", parentId: "1" },
]


const contains = (arr, obj) => {
  let i = arr.length;
  while (i--) {
     if (JSON.stringify(arr[i]) === JSON.stringify(obj)) {
         return true;
     }
  }
  return false;
}

const isEqual = (obj1, obj2) => {
  let n = 0
  if (obj1.length !== obj2.length) {
      return false;
  }
  for (let i = 0; i < obj1.length; i++) {
      if (contains(obj2, obj1[i])) {
        n++
      }
  }
  return n === obj1.length
}

console.log(isEqual(collection1,collection2))

如果考虑到项目的顺序,请使用 lodash 中的内置函数 isEqual

评论

0赞 Ben Racicot 7/12/2022
您可能只想进行一次测试,而不是每次都进行测试。JSON.stringify(obj)
1赞 Heretic Monkey 8/10/2022
将你的一个对象更改为具有不同的顺序(例如),并观察这个中断。{ parentId, name, id }
-3赞 C-lio Garcia 7/3/2022 #18

如果你把它们串起来......

type AB = {
  nome: string;
}

const a: AB[] = [{ nome: 'Célio' }];
const b: AB[] = [{ nome: 'Célio' }];

console.log(a === b); // false
console.log(JSON.stringify(a) === JSON.stringify(b)); // true

评论

1赞 Heretic Monkey 8/10/2022
对于具有具有基元值的单个属性的简单对象,这确实有效。尝试使用两个数组,其中包含具有多个不同顺序的属性的对象(例如),您会发现它不再有效。这在尝试相同技巧的几个早期答案中进行了讨论(例如 stackoverflow.com/a/52842903const a = [{ nome: 'Célio', age: 20 }]; const b = [{ age: 20, nome: 'Célio' }])
1赞 user8453321 2/1/2023
如果两个对象数组上的数据顺序不同,则此操作将不起作用。