Verify that the values within an array are the same or different

EDWARD FABIAN TAPIERO GOMEZ

I need to find matrices where all the values are the same. How should I go through the array to compare the values?

Example true:

[{"id": 1 ,"value": cow},{"id": 1 ,"value": cow},{"id": 1 ,"value": cow}] // true

Example false:

[{"id": 1 ,"value": cow},{"id": 2 ,"value": cat},{"id": 1 ,"value": cow}] // false

Thanks

Sebastian Kaczmarek

You can just compare every element of the array with the first one and if all of them are equal to it, then it means that every element in the array is the same:

const input = [{"id": 1 ,"value": 'cow'},{"id": 1 ,"value": 'cow'},{"id": 1 ,"value": 'cow'}];

const [ first, ...rest ] = input;

const result = rest.every((entry) => entry.id === first.id && entry.value === first.value);

console.log(result);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

PHPDoc - Different objects with different class within an array

분류에서Dev

How to return same values of the array

분류에서Dev

Sane way to store different data types within same column in postgres?

분류에서Dev

Sane way to store different data types within same column in postgres?

분류에서Dev

How to compare two different values of different functions or same function?

분류에서Dev

Push values to the same index of an array in PHP

분류에서Dev

PHP How to sum values of the array of the same key

분류에서Dev

Group array values that are in the same range of multiples of 10

분류에서Dev

Compare array values with same key in PHP

분류에서Dev

input values into an array, using different class files

분류에서Dev

Update the array with attributes from different array with same key for both - javascript

분류에서Dev

Splitting one Pandas column on values from a different column in the same row?

분류에서Dev

LocationManager giving 2 different values for Lat and Long for same location

분류에서Dev

selecting all checkboxes in javascript - with array of different values for checkbox

분류에서Dev

How can I determine if all the values in an array are different, in Matlab

분류에서Dev

How to verify if a key is into array in Behat

분류에서Dev

StandardSQL BigQuery values from key:value pairs within an Array into separate columns. How?

분류에서Dev

How to set variable values for different statement for same NOT FOUND SQL EXCEPTION in STORE PROCEDURE

분류에서Dev

need to access 3 values in a php array that contains 20, is a 3d array and I need to access the same values 60 times

분류에서Dev

Call a variable within an array

분류에서Dev

input array within jquery

분류에서Dev

Calculatin within an Array

분류에서Dev

Interactivity within the same graph - ggvis

분류에서Dev

How to get clicked element from multiple element with the same name different index index of array

분류에서Dev

Converting values within ORDER BY clause

분류에서Dev

Same Variable, different result

분류에서Dev

same trigger for different events

분류에서Dev

Same strings are different

분류에서Dev

Different results for same numbers

Related 관련 기사

  1. 1

    PHPDoc - Different objects with different class within an array

  2. 2

    How to return same values of the array

  3. 3

    Sane way to store different data types within same column in postgres?

  4. 4

    Sane way to store different data types within same column in postgres?

  5. 5

    How to compare two different values of different functions or same function?

  6. 6

    Push values to the same index of an array in PHP

  7. 7

    PHP How to sum values of the array of the same key

  8. 8

    Group array values that are in the same range of multiples of 10

  9. 9

    Compare array values with same key in PHP

  10. 10

    input values into an array, using different class files

  11. 11

    Update the array with attributes from different array with same key for both - javascript

  12. 12

    Splitting one Pandas column on values from a different column in the same row?

  13. 13

    LocationManager giving 2 different values for Lat and Long for same location

  14. 14

    selecting all checkboxes in javascript - with array of different values for checkbox

  15. 15

    How can I determine if all the values in an array are different, in Matlab

  16. 16

    How to verify if a key is into array in Behat

  17. 17

    StandardSQL BigQuery values from key:value pairs within an Array into separate columns. How?

  18. 18

    How to set variable values for different statement for same NOT FOUND SQL EXCEPTION in STORE PROCEDURE

  19. 19

    need to access 3 values in a php array that contains 20, is a 3d array and I need to access the same values 60 times

  20. 20

    Call a variable within an array

  21. 21

    input array within jquery

  22. 22

    Calculatin within an Array

  23. 23

    Interactivity within the same graph - ggvis

  24. 24

    How to get clicked element from multiple element with the same name different index index of array

  25. 25

    Converting values within ORDER BY clause

  26. 26

    Same Variable, different result

  27. 27

    same trigger for different events

  28. 28

    Same strings are different

  29. 29

    Different results for same numbers

뜨겁다태그

보관