如何找到要插入数组数组的索引

How to find the index to be insterted in array of array

如何找到要在对象数组中插入的索引。

我有对象数组。

var cordinate =  [
[225, 242],
[405, 242],
[585, 242],
[765, 242],
[225, 325],
[405, 325],
[585, 325],
[765, 325],
[225, 408],
[405, 408],
[585, 408],
[765, 408]
]

这里我想插入最近的元素

我要插入的数组

var extraEle = [404, 260]

如何确定坐标中extraEle的确切索引。

这是我正在尝试的

我正在比较 y 坐标以获得最近的范围,然后将 x 坐标与仅 y 范围进行比较以获得索引。

 var yValue = [];
var diffVal = cordinate[0][1];
for(var i=0; i<cordinate.length;i++){
    if (Math.abs(extraEle[1] - diffVal) > Math.abs(extraEle[1]- cordinate[i][1])){
        diffVal = componentsPos[i][1];
    }
}
var index;
yvalue = [];
for(var i=0; i<cordinate.length;i++){
    if (cordinate[i][1] === diffVal){
        yvalue.push(componentsPos[i]);
    }
}

var diffValX = yvalue[0][0];
for(var i=0; i<yvalue.length;i++){
    if (Math.abs(extraEle[0] - diffValX) > Math.abs(extraEle[0]- yvalue[i][0])){
        diffValX = yvalue[i][0];
    }
}

var indexValue = [diffValX,diffVal]
cordinate.indexOf(indexValue, 0)

我们能有比这更好的内置方法吗? 注意:此代码是工作代码。

我的输出将是 2 因为 260(Y) 将在前 4 个元素和 404 (X) 会出现在第三个元素之前。

添加坐标后应该是这样的。

cordinate =  [
[225, 242],
[405, 242],
[404, 260],
[585, 242],
[765, 242],
[225, 325],
[405, 325],
[585, 325],
[765, 325],
[225, 408],
[405, 408],
[585, 408],
[765, 408]

]

但准确地说,我只需要可以插入的索引。

这可能不是最优雅的解决方案,但它会确定最近的坐标,通过计算点之间的距离并确定最近的坐标,然后您可以 splice 将其放入数组中。

// Input coordinates
let coordinates = 
[
  [225, 242],
  [405, 242],
  [585, 242],
  [765, 242],
  [225, 325],
  [405, 325],
  [585, 325],
  [765, 325],
  [225, 408],
  [405, 408],
  [585, 408],
  [765, 408]
];

// Add this to the array
let extraEle = [404, 260];

getClosestIndex(coordinates, extraEle).then((result) => {
  coordinates.splice(result, 0, extraEle);
  console.log(coordinates);
  // Output
  /*[
      [225, 242],
      [405, 242],
      [404, 260]
      [585, 242],
      [765, 242],
      [225, 325],
      [405, 325],
      [585, 325],
      [765, 325],
      [225, 408],
      [405, 408],
      [585, 408],
      [765, 408]
    ]; */
});

function getClosestIndex(coords, targetCoords) {
  return new Promise((resolve, reject) => {    
    let shortestDist = 999999999;
    let shortestIndex = 0;
    coords.forEach((coord, index) => {  
      let dist = Math.sqrt( Math.pow((coord[0]-targetCoords[0]), 2) 
                           + Math.pow((coord[1]-targetCoords[1]), 2));
      if (dist < shortestDist) {
          shortestDist = dist;
          shortestIndex = index;
      } 
    });
     // To add it after the closest element
    if (shortestIndex < coords.length - 1)
      shortestIndex += 1;
    resolve(shortestIndex);
  });
}

我理解的逻辑是:

找到最接近y值的子数组,然后X更准确。注意:OP 的期望输出示例是错误的:

[405, 242], [404, 260], [585, 242],...

[404, 260]应该放在[405, 242].

之前

在下面的例子中:

  • 2 个数组数组是根据 x 和 y 值创建的。
[[x, (x - 404)],...] and [[y, (y - 260)],...] 

然后每个子数组按索引 1 排序,并从中得出最接近的 [x, y]

let coords = [[225,242],[405,242],[585,242],[765,242],[225,325],[405,325],[585,325],[765,325],[225,408],[405,408],[585,408],[765,408]];

let add = [404, 260];

const findRange = (array, xy) => {
  let xArr = [];
  let yArr = [];
  for (let sub of array) {
    xArr.push([sub[0], Math.abs(xy[0] - sub[0])]);
    yArr.push([sub[1], Math.abs(xy[1] - sub[1])]);
  }
  let xRng = xArr.sort((a, b) => a[1] - b[1]);
  let yRng = yArr.sort((a, b) => a[1] - b[1]);
  let X = xRng[0][0];
  let Y = yRng[0][0];
  let closest = [X, Y];
  let idx = array.flatMap((s, i) => closest[0] === s[0] && closest[1] === s[1] ? i : []); 
  if (X >= xy[0]) {
    array.splice(idx, 0, xy);
  } else {
    array.splice(idx+1, 0, xy);
  }
  return array;
};
  
    
console.log(findRange(coords, add));

这是一个有趣的解决方案。

给定一个在数组中查找最接近值的简单函数,找到最接近的 Y 值。然后你所要做的就是将新坐标添加到数组中并按 Y 然后 X 列排序。对新坐标进行排序时,将其 Y 值视为最接近的 Y 值。

let coords = [[225,242],[405,242],[585,242],[765,242],[225,325],[405,325],[585,325],[765,325],[225,408],[405,408],[585,408],[765,408]];
let add = [404, 260];

const closestValue = (array, goal) => 
   array.reduce((prev, curr) => 
    Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);

let nearestY = closestValue(coords.map(c => c[1]), add[1]);

coords.push(add);
coords.sort(function([a, b], [c, d]) {
  if(b === add[1]){
    b = nearestY;
  } else if(d === add[1]){
    d = nearestY;
  }  
  return b - d || a - c   
});

console.log(coords);

我相信排序可以改进。