简体   繁体   English

根据元素的数字顺序合并两个数组

[英]Merging two arrays based on element's numerical order

I try to came up to algorithm for parsing array. 我试图找到解析数组的算法。 What it should do: in input in takes two arrays first_array=[[10,25], [30,45], [50,60]] and second_array=[35, 40] in output it should return one array [[10,25], [30,35], [40,45], [50,60]] . 它应该做什么:在输入中取两个数组first_array=[[10,25], [30,45], [50,60]]second_array=[35, 40]输出它应该返回一个数组[[10,25], [30,35], [40,45], [50,60]] As you guessed it takes second_array[1] and compare with first_array[each][1] and creates subarray. 正如你猜测它需要second_array[1]并与first_array[each][1]进行比较并创建子first_array[each][1] So far I got (Code Below). 到目前为止我得到了(代码如下)。 Some how in if statement I need two returning. 一些如何在if语句中我需要两个返回。 Second argument also could be a multiple array. 第二个参数也可以是多个数组。 在此输入图像描述

 /*---- First part of problem ----*/ const first_array=[[10,25], [30,45], [50,60]]; const second_array=[35, 40]; const result = first_array.map(record => { if (second_array[0] > record[0] && second_array[0] < record[1]) { return [[record[0], second_array[0]], [second_array[1], record[1]]] } else { return record } }); console.log(result) // result [ [10,25] , [[30,35],[40,45]] , [50,60] ] // expect [ [10,25] , [30,35] , [40,45] , [50,60] ] /*---- Second part of problem ----*/ const first_array=[15, 60]; const second_array=[[25,30], [30,40], [45,55]]; // [[15, 60]] // [[15, 25], [30, 60]] // [[15, 25], [30, 30], [40, 60]] //output// [[15, 25], [30, 30], [40, 45], [55, 60]] 

在此输入图像描述

first I join the arrays and after that sorted and split them 首先我加入数组,然后对它们进行排序和拆分

var a=[[10,25], [30,45], [50,60]]
var b=[35, 40];

var sorted=[].concat.apply([], a,b).sort();

var pairs = [];

for(var i = 0; i < sorted.length; i += 2)
{
    pairs.push(sorted.slice(i, i + 2));
}

You could use a Generator for the arrays and take the same generator for nested arrays. 您可以为数组使用Generator ,并为嵌套数组使用相同的生成器。

Then check if the values are in order and update the result array by checking the lenght of the collector array. 然后检查值是否有序并通过检查收集器数组的长度来更新结果数组。

Proceed until both generators return done . 继续,直到两个发电机都完成

 function combine(leftArray, rightArray) { function* getElements(array) { var i = 0; while (i < array.length) { if (Array.isArray(array[i])) { yield* getElements(array[i]) } else { yield array[i]; } i++; } } function update(value) { temp.push(value); if (temp.length === 2) { result.push(temp); temp = []; } } var left = getElements(leftArray), right = getElements(rightArray), l = left.next(), r = right.next(), temp = [], result = []; while (!l.done || !r.done) { if (r.done || !l.done && l.value < r.value) { update(l.value); l = left.next(); continue; } if (l.done || !r.done && r.value < l.value) { update(r.value); r = right.next(); } } if (temp.length) { result.push(temp); } return result; } console.log(combine([[10, 25], [30, 45], [50, 60]], [[12, 42], [67, 69]])); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Foor unsorted arrays, you could flat the arrays, sort and group them. 对于未排序的数组,您可以对阵列进行平坦处理,对其进行排序和分组。

 function combine(left, right) { const flat = (r, a) => Array.isArray(a) ? a.reduce(flat, r) : [...r, a]; return [left, right] .reduce(flat, []) .sort((a, b) => a - b) .reduce((r, a, i) => (i & 1 ? r[r.length - 1].push(a) : r.push([a]), r), []); } console.log(combine([[20, 19], [30, 45], [9, 5]], [[12, 42], [67, 69]])); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Here is an interesting approach which i expect to be fast. 这是一个有趣的方法,我期望快速。

We have an array scaner function function which works like a map. 我们有一个数组scaner函数函数,它像地图一样工作。 Takes a function which modifies each element while updating an accumulator which is kept under closure. 采用一个函数来修改每个元素,同时更新一个保持在关闭状态的累加器。 The function we use here is a sorter which takes to size 2 arrays and order the smallest items in one and the rest in the other. 我们在这里使用的函数是一个分拣机,它采用2个数组的大小,并将最小的项目排在一个,其余项目在另一个中。 So sorter([1,5], [3,7]) will result [1,3] and accumulator will change to [5,7] . 因此sorter([1,5], [3,7])将导致[1,3]并且累加器将变为[5,7] Here it is; 这里是;

 var first_array = [[10,25], [30,45], [50,60]], second_array = [28,46], merger = (a,f,t) => a.map((t => e => f(e,t))(t)).concat([t]), sorter = (a,b) => a[1] < b[1] ? a[1] < b[0] ? a : a[0] < b[0] ? ([a[1],b[0]] = [b[0],a[1]], a) : ([a[1],b[0]] = [b[0],a[1]], a.reverse()) : a[0] > b[1] ? ([a[0],b[0]] = [b[0],a[0]], [a[1],b[1]] = [b[1],a[1]], a) : a[0] > b[0] ? ([a[0],b[1]] = [b[1],a[0]], [a[0],b[0]] = [b[0],a[0]], [a[1],b[1]] = [b[1],a[1]], a) : ([a[1],b[0]] = [b[0],a[1]], b.reverse(), a), result = merger(first_array,sorter,second_array); console.log(JSON.stringify(result)); 

Use reduce instead of map 使用reduce而不是map

const first_array=[[10,25], [30,45], [50,60]];
const second_array=[35, 40];

const result = first_array.reduce((a, record) => {
  if (second_array[0] > record[0] && second_array[0] < record[1]) {
    a.push([record[0], second_array[0]])
    a.push([second_array[1], record[1]])
  } else {
    a.push(record)
  }

  return a
}, [])

console.log(result)

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

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