如何在javascript中比较一个已排序的降序数组和一个未排序的数组以及已排序的数组中未排序的数组元素的位置。
因此,如果排序数组中的元素数为7
[100,90,80,70,60,50,40]未排序数组中的元素数为4,未排序数组为
[200,10,55,65]则输出将为
1
8
6
5发布于 2018-08-16 03:18:06
看起来您想要找到每个元素在排序数组中适合的位置的索引(从1开始)。您应该能够使用map()和findIndex()做到这一点
let arr = [100,90,80,70,60,50,40]
let a2 = [200,10,55,65]
let indexes = a2.map(n => {
// find the first place in arr where it's less than n
let ind = arr.findIndex(i => i < n)
// if n wasn't found, it is smaller than all items: return length + 1
return (ind === -1) ? arr.length + 1 : ind + 1
})
console.log(indexes)
https://stackoverflow.com/questions/51864906
复制相似问题