简体   繁体   English

如何根据它们的值加入两个 arrays

[英]How to join two arrays according to their values

I have the following arrays:我有以下 arrays:

var main_arr = [[54.5,225.5], [45.25,15.54], [1.5, 22.3]];
var sec_arr = [[1.51, 22.35], [54.32,32.54], [4.56,45.45]];

I want to get the last element of the first array and find the closest one by values from the second array.我想获取第一个数组的最后一个元素,并通过第二个数组中的值找到最接近的元素。

Then I want the found element to be pushed in the main array and removed from the secondary.然后我希望将找到的元素推入主数组并从辅助数组中删除。 I want it to repeat until there are no elements in the secondary array.我希望它重复,直到辅助数组中没有元素。

The result should look like this:结果应如下所示:

[[54.5,225.5], [45.25,15.54], [1.5, 22.3], [1.51, 22.35], [4.56,45.45], [54.32,32.54]]

How can I do it using pure JavasScript without any frameworks?如何在没有任何框架的情况下使用纯 JavaScript 来做到这一点?

Edit:编辑:

I'm not sure if I explained it well enough so I'm gonna give you a simpler version.我不确定我是否解释得足够好,所以我会给你一个更简单的版本。 You have one main and one secondary array of numbers.您有一个主要和一个辅助数字数组。 You can't reorder anything in the main one.您无法重新排序主目录中的任何内容。

var main = [1, 5, 4, 3, 7];
var secondary = [9, 3, 4, 0, 5];

I want to get the number that is the closest to the last element of the main array and push it to it.我想获取最接近主数组最后一个元素的数字并将其推送到它。 Then it should be repeated until there are no elements in the secondary array.然后应该重复它,直到辅助数组中没有元素。 The result should look like this:结果应如下所示:

var main = [1, 5, 4, 3, 7, 9, 5, 4, 3, 0];
var secondary = [];

Example例子

I'll give a code sample first and elaborate a bit on it later on:我将先给出一个代码示例,稍后再详细说明:

let main = [1, 5, 4, 3, 7];
let secondary = [9, 3, 4 0, 5];

for (var i = secondary.length; i > 0; i--) {
    const lastElement = main[main.length-1];
    const distance = secondary.map(item => calculateDistance(item, lastElement));
    const index = indexOfMinValue(distance);
    main.push(secondary[index]);
    secondary.splice(index, 1);
}

function calculateDistance(item, lastElement) {
    // calculate the distance (e.g. (x1-x2)^2 + (y1-y2)^2)
}

function indexOfMinValue(distance) {
    return distance.reduce((iMin, x, i, arr) => x < arr[iMin] ? i : iMin, Number.POSITIVE_INFINITY);
}

What it does它能做什么

Since you move exactly one item from the secondary array in each iteration you have to iterate over the length of the secondary array (you can not use for (var i = 0; i < secondary.length; i++) since the length will change in each iteration.由于您在每次迭代中从辅助数组中仅移动一个项目,因此您必须迭代辅助数组的长度(您不能使用for (var i = 0; i < secondary.length; i++) ,因为长度会在每次迭代。

You then have to calculate the distances of all elements in regard to the last element of the main array.然后,您必须计算所有元素相对于主数组的最后一个元素的距离。 For mere numbers you would do just a simple comparison abs(lastElement - currentElement) for points you would have to use the formula as suggested in the comment.对于单纯的数字,您只需对点进行简单的比较abs(lastElement - currentElement)就必须使用评论中建议的公式。

With that you get an array that holds the current distances from which you have to find the minimum value.这样,您将获得一个数组,其中包含您必须从中找到最小值的当前距离。 In the given example in the first iteration the distance array would look like this:在第一次迭代的给定示例中,距离数组如下所示:

distance = [2, 4, 3, 7, 2];

The indexOfMinValue -function (taken from this answer ) yields the index of the element with the minimum distance (the first one in this example). indexOfMinValue -函数(取自this answer )产生具有最小距离的元素的索引(本例中的第一个)。 With that index you can push the corresponding value from the secondary to the main array and remove it from the secondary array.使用该索引,您可以push相应的值从辅助数组推送到主数组,并将其从辅助数组中删除。

Considerations注意事项

  • The complexity of the algorithm is O(n^2) where n is the number of elements in the secondary array.该算法的复杂度为 O(n^2),其中 n 是辅助数组中的元素数。 So this might get out of hand for huge arrays.因此,对于巨大的 arrays,这可能会失控。
  • In its current form the indexOfMinValue yields the first element with minimum distance if several items have the same distance.如果多个项目具有相同的距离,则indexOfMinValue在其当前形式中产生具有最小距离的第一个元素。 It might be desired to yield the last result or select one of the minima randomly or...可能需要随机产生最后一个结果或 select 最小值之一或...

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

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