Javascript - Generating all combinations of elements in a single array (in pairs)

后端 未结 9 647
天命终不由人
天命终不由人 2020-11-28 11:14

I\'ve seen several similar questions about how to generate all possible combinations of elements in an array. But I\'m having a very hard time figuring out how to write an a

9条回答
  •  感动是毒
    2020-11-28 11:48

    Using map and flatMap the following can be done (flatMap is only supported on chrome and firefox)

    var array = ["apple", "banana", "lemon", "mango"]
    array.flatMap(x => array.map(y => x !== y ? x + ' ' + y : null)).filter(x => x)
    

提交回复
热议问题