我想比较同一数组中的元素以获得此结果
let arr = ['one','two','three','four'];
arr.forEach((ele, i, ar)=>{
console.log(ele, i, ar)
for(let item = i; item< ar.length; item++ ){
console.log (ele, ar[item])
}
})
'one' => 'two',
'one' => 'three',
'one' => 'four',
'two' => 'three',
'two' => 'four',
'three' => 'four'.
最佳答案
可以使用两个for循环,让第二个循环从第一个循环的索引+1开始:
let arr = ['one','two','three','four'];
for(let i=0; i< arr.length; i++){
for(j=i+1; j<arr.length; j++){
console.log(arr[i],' => ', arr[j]);
}
}
关于javascript - 如何将第一个元素与其余元素进行比较,以及如何将第二个元素与其余元素进行比较以及相同的重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65266713/