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
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] 삭제
몇 마디 만하겠습니다