For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/proposal-array-equality.md.
  • 简体中文
  • Array Equality S1

    中文标题:数组相等性

    提案概览
    提案速览

    该提案解决了缺少标准方法来比较数组相等性,特别是深度嵌套数组的问题。它提议添加一个 Array.prototype.equals 方法,执行深度比较。该提案处于早期阶段,未提及实现细节或标准化进展。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    数组相等性

    确定两个数组是否相等。

    作者: Hemanth HM

    冠军: Jordan Harband & Hemanth HM

    动机:

    今天我们没有一个直接的方法来检查两个数组是否相等,当它们是深度嵌套的数组时,检查变得更加混乱。

    目前如何处理?

    没有标准的方法,有一些模块,如 array-equaldeep-equal,它们的下载量分别为每周约 500 万次和 800 万次,这是深度比较的一个子集。

    考虑几个例子:

    // Schema validation
    const equal = require('deep-equal');
    
    const expectedSchema = {
      name: {
        type: String,
        required: true
      },
      score: {
        type: Number,
        default: 0
      }
    };
    
    equal(schemaCall.args[0], expectedSchema);
    

    node 内置:

    // assert.deepStrictEqual(actual, expected[, message])
    
    deepStrictEqual([new Uint32Array([1, 2, 3, 4]).subarray(1, 3), new Uint32Array([2, 3])]);
    // assert.deepEqual(actual, expected[, message])
    
    assert.deepEqual( tokenizer( "AD", "G", cldr ), [{
    		type: "G",
    		lexeme: "AD",
    		value: "1"
    	}] );

    提案:

    有一个 Array.prototype.equals 方法,可能看起来像这样:

    [1,2,3].equals([1,2,3]) // 计算结果为 true
    
    [1,2,undefined].equals([1,2,3]) // 计算结果为 false。
    [1, [2, [3,4]]].equals([1, [2, [3,4]]]) // true
    [{
      foo: 'bar'
    }, {
      foo: 'baz'
    }].equals[{
        foo: 'bar'
    }] // false
    

    其他语言如何处理这个?

    Ruby: array1.to_set == array2.to_set

    Python: [1,[2,3]] == [1,[2,3]]

    Java: java.util.Arrays.equal

    C#: Enumerable.Except 或者 SequenceEqual