JavaScript - 比较具有有限值/元素的两个数组

时间:2022-02-24 12:06:58

I have an array of a few numbers or strings, eg.

我有一些数字或字符串的数组,例如。

[4.3, 1.2, 4.5, 3.9]

['I', 'the', 'sheriff', 'shot']

The users re-arrange the elements in a specific order. For example, in an ascending order:

用户按特定顺序重新排列元素。例如,按升序排列:

[1.2, 3.9, 4.3, 4.5]

and to create a valid sentence:

并创建一个有效的句子:

['I', 'shot', 'the', 'sheriff']

I have those predefined answers stored in an object. How do I compare the arrays with user generated order with the ones with correct order of elements. I have seen other answers on how to compare arrays but my question does not require a wide range of 'what if....' that other answers had. The elements are generated by the app so no unexpected input would be there, no undefined values, no empty elements, etc. Users do not input anything. They just rearrange tiles with those elements.

我将这些预定义的答案存储在一个对象中。如何将具有用户生成顺序的数组与具有正确元素顺序的数组进行比较。我已经看到了关于如何比较数组的其他答案,但我的问题并不需要其他答案所具有的各种“如果......”。这些元素由应​​用程序生成,因此不存在任何意外输入,没有未定义的值,没有空元素等。用户不输入任何内容。他们只是用这些元素重新排列瓷砖。

What's the best way to compare the arrays to determine if the users have rearranged the boxes in the right way.

比较数组以确定用户是否以正确的方式重新排列框的最佳方法是什么。

My first attempt is incorrect as it always seems to be outputing 'BINGO' even if the order is incorrect:

我的第一次尝试是不正确的,因为它总是似乎输出'BINGO',即使订单不正确:

if (this.props.squares === correctOrder) {
      console.log("BINGO");
    }

4 个解决方案

#1


4  

I would use every function which all arrays have.

我会使用所有数组都具有的每个函数。

const isCorrect = this.props.squares.every((square, index) =>
  correctOrder[index] === square);

#2


2  

You could store the wanted order in an object, where the values of the first array are the keys and ththe sorted indices the values.

您可以将所需订单存储在对象中,其中第一个数组的值是键,而排序的索引是值。

For checking if in right order, just check the items array with the objects value and the actual index.

要检查是否按正确顺序,只需使用对象值和实际索引检查items数组。

var items = [1.2, 3.9, 4.3, 4.5],
    order = items.reduce((o, k, i) => (o[k] = i, o), {});
    
// check
items.every((k, i) => order[k] === i) && console.log('bingo');

#3


1  

The easiest way would be to iterate through all the elements and compare:

最简单的方法是迭代所有元素并进行比较:

function arraysEqual(array1, array2) {
    for(var i = array1.length; i--;) {
        if(array1[i] !== array2[i])
            return false;
    }

    return true;
}

Ex:

例如:

arraysEqual(["I", "am", "well"], ["I", "am", "not", "well"])  // returns false
arraysEqual(["I", "am", "well"], ["I", "am", "well"])  // returns true

Hope it helps.

希望能帮助到你。

#4


0  

If you don't mind using lodash, this also could be an option.

如果您不介意使用lodash,这也可以是一个选项。

_.isEqual(this.props.squares, correctOrder);

#1


4  

I would use every function which all arrays have.

我会使用所有数组都具有的每个函数。

const isCorrect = this.props.squares.every((square, index) =>
  correctOrder[index] === square);

#2


2  

You could store the wanted order in an object, where the values of the first array are the keys and ththe sorted indices the values.

您可以将所需订单存储在对象中,其中第一个数组的值是键,而排序的索引是值。

For checking if in right order, just check the items array with the objects value and the actual index.

要检查是否按正确顺序,只需使用对象值和实际索引检查items数组。

var items = [1.2, 3.9, 4.3, 4.5],
    order = items.reduce((o, k, i) => (o[k] = i, o), {});
    
// check
items.every((k, i) => order[k] === i) && console.log('bingo');

#3


1  

The easiest way would be to iterate through all the elements and compare:

最简单的方法是迭代所有元素并进行比较:

function arraysEqual(array1, array2) {
    for(var i = array1.length; i--;) {
        if(array1[i] !== array2[i])
            return false;
    }

    return true;
}

Ex:

例如:

arraysEqual(["I", "am", "well"], ["I", "am", "not", "well"])  // returns false
arraysEqual(["I", "am", "well"], ["I", "am", "well"])  // returns true

Hope it helps.

希望能帮助到你。

#4


0  

If you don't mind using lodash, this also could be an option.

如果您不介意使用lodash,这也可以是一个选项。

_.isEqual(this.props.squares, correctOrder);