我有一個陣列
const array = [[1, 2], [3, 3]]; arr.forEach(el => {
if (el[0] === el[1]) {return true}
})
uj5u.com熱心網友回復:
你應該every發揮作用
const arr = [[1, 2], [3, 3]];
const b = arr.every(el => el[0] === el[1]);
console.log(b); // false
uj5u.com熱心網友回復:
這應該會得到想要的結果。如果它們彼此相等,只需將 console.log(true) 更改為您想做的任何事情。這適用于任何長度的陣列。
const array = [
[1, 2],
[3, 3],
];
array.forEach((el) => {
if (el[0] === el[el.length - 1]) {
console.log(true);
}
});
uj5u.com熱心網友回復:
map 在陣列上生成一個新的真/假值陣列,確定元素是否匹配。
const out = [[1, 2], [3, 3]].map(arr => {
return arr[0] === arr[1];
});
console.log(out);
uj5u.com熱心網友回復:
在 forEach 中回傳 true 沒有意義
我想你的意思是:
const testArr = arr => arr[0] === arr[arr.length-1]; // test first and last
const array = [[1, 2], [3, 3]];
const testResults = array.map(el => testArr(el)); // map the results
console.log(testResults)
// filter:
const same = array.filter(el => testArr(el)); // FIND the array that has same first and last
console.log(same.flat())
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/362813.html
標籤:javascript 数组
下一篇:ES12:空合并運算子和物件
