简体   繁体   English

如何在 map 两个 arrays 基础上比较值

[英]How to map two arrays based on comparative value

I have two arrays:我有两个 arrays:

A sequential array of numbers between 1-8, in increments of 1:一个 1-8 之间的连续数字数组,以 1 为增量:

a = [1,2,3,4,5,6,7,8]

A sequential array of numbers between 1-8, with random increments: 1-8 之间的连续数字数组,随机增量:

b = [1,2,5,7]

I want to create a list of {a:val, b:val} dict pairs, where the value of b is only the next in the array if it is equal or greater to the value of a :我想创建一个{a:val, b:val}字典对的列表,其中b的值仅是数组中的下一个,如果它等于或大于a的值:

c = [
       {a:1, b:1}, 
       {a:2, b:2}, 
       {a:3, b:2}, 
       {a:4, b:2}, 
       {a:5, b:5}, 
       {a:6, b:5}, 
       {a:7, b:7}, 
       {a:8, b:7}
   ]

Is there an easy way to do this?是否有捷径可寻? I thought about using compounding $.each loops to build new arrays and increment values, but it seems as though this is overkill?我考虑过使用复合$.each循环来构建新的 arrays 和增量值,但这似乎有点矫枉过正?

You can use map and shift您可以使用mapshift

  • Loop over first array.循环第一个数组。
  • If the value of current element is greater then first element on second array remove first element from second array, ( except when the length is less then 2)如果当前元素的值大于第二个数组的第一个元素,则从第二个数组中删除第一个元素(除非长度小于 2)
  • return object in desired format以所需格式返回 object

 let a = [1, 2, 3, 4, 5, 6, 7, 8] let b = [1, 2, 5, 7] let final = a.map(v => { if (v >= b[1] && b.length > 1) { b.shift() } return { a: v, b: b[0] } }) console.log(final)

This mutates second array if you don't want to mutate you can use a index variable and increment it based on condition如果您不想变异,这会变异第二个数组,您可以使用索引变量并根据条件递增它

 let a = [1, 2, 3, 4, 5, 6, 7, 8] let b = [1, 2, 5, 7] let index = 0 let final = a.map(v => { if (v >= b[index+1] && b.length - 1 > index ) { index++ } return { a: v, b: b[index] } }) console.log(final)

The first array is actually redundant - the only significant information it carries is the number of elements.第一个数组实际上是多余的——它携带的唯一重要信息是元素的数量。

The following code determines the difference between 2 adjacent threshold values from array b .以下代码确定数组b中 2 个相邻阈值之间的差异。 This is 1 less the number of array elements with object property b set to the lower of these threshold values.这是将 object 属性b设置为这些阈值中较低值的数组元素的数量减 1。

The target array is constructed as the array of object properties b in proper order.目标数组按正确顺序构造为 object 属性b的数组。 The resulting value list is mapped to the desired format.结果值列表被映射到所需的格式。

The code may be optimized.代码可能会被优化。 It should run fast if the length of array b is much less than the length of array a .如果数组b的长度远小于数组a的长度,它应该运行得很快。 This hypothesis is not tested though and given the low complexity of the code/data structures will probably be insignificant.虽然这个假设没有经过测试,并且考虑到代码/数据结构的低复杂性可能是微不足道的。

 let a = [1, 2, 3, 4, 5, 6, 7, 8], b = [1, 2, 5, 7], c = [], n_last = 1; b.forEach ( (pn_current, pn_idx) => { c.push(...((new Array(pn_current - n_last)).fill(n_last)) ); n_last = pn_current; }); c.push(...((new Array(a.length + 1 - n_last)).fill(n_last)) ); c = c.map ( ( pn_entry, pn_idx ) => { return { a: pn_idx+1, b: pn_entry }; } ); console.log(c);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM