如何将第一个元素与其余元素进行比较,将第二个元素与其余元素进行比较以及相同的重复

How can the first element be compared to the rest of the elements and the second element to the rest of the elements and the same repetition

我想比较同一数组中的元素以获得此结果

      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]);
  }
  }

使用flatMapslice

let arr = ["one", "two", "three", "four"];

const res = arr.flatMap((x, i) => arr.slice(i + 1).map(y => `${x} => ${y}`));

console.log(res)