【问题标题】:Javascript: Manipulate array to return shortest nodeJavascript:操作数组以返回最短节点
【发布时间】:2021-06-07 01:19:49
【问题描述】:

我有一个数组如下

var a = [ [ 5, 5, 1, -4 ], [ 3, 7, 3, -1 ], [ 7, 3, 4, 1 ], [ 5, 5, 5, 0 ] ]

每个嵌套数组 index:2 元素表示它与 0(零)的距离,每个 index:3 元素表示它与下一个轮询点的距离。

我正在尝试对这个数组进行排序,以便我可以得到嵌套数组,它参考 index:2 元素接近 index:0,并且它的下一个轮询点非常接近。

例如,我这里的答案是[ 3, 7, 3, -1 ],因为

  • 虽然 [ 5, 5, 1, -4 ] , index:2 非常接近 0,但它的下一个点位于 4 个位置之后/之前。但是对于[ 3, 7, 3, -1 ],下一个投票点在一个位置。

我尝试了如下排序

js

inDiff = inDiff.sort( function(a,b ){      
      if ( a[2] < b[2]) {
        if ( Math.abs(a[3]) < Math.abs(b(3)) ){
          return Math.abs(b[3]) - Math.abs(a[3]);
        }
      }
    });

更新 1

正如评论中所问的,我正在为嵌套数组的每个元素添加解释。例如在嵌套数组 [ 5, 5, 1, -4 ]

索引:0:值 5 代表我正在寻找的第一个数字

索引:1 值 5 表示第二个数字(下一个轮询点编号)

通过将这两个数字相加,我将实现找到两个总和为 10 的数字的要求。

索引 2 : 值 1 : 表示源数组中第一个数字的索引只有 5

Index 3 : Value -4 : 表示 Index:0 和 Index:1 的索引与源数组的嵌套数组数之间的差异。

但是我的数组没有任何反应。 任何帮助,非常感谢。

【问题讨论】:

  • 你能不能更清楚地布局子数组中的每个索引代表什么?
  • @Kwright02 更新了我的问题,请您现在查看。谢谢。

标签: javascript arrays


【解决方案1】:

假设输入始终遵循此演示使用的要求.reduce()

const AA = [
  [5, 5, 1, -4],
  [3, 7, 3, -1],
  [7, 3, 4, 1],
  [5, 5, 5, 0]
];

let result = AA.reduce((min, now, idx, AoA) => {
  let distTo0 = array => Math.abs(Math.floor(array[2]) + Math.floor(array[3]));
  min = distTo0(now) < distTo0(min) ? now : min;
  return min;
});

console.log(result);

以下演示包含我理解的所有规则:

const crazyLogic = arrayOfArrays => {
  let AAClone = JSON.parse(JSON.stringify(arrayOfArrays));
  const shared = AAClone[0][0] + AAClone[0][1];
  const rules = [`Must be an array of number arrays`, `The sum of index 0 and 1 of each sub-array must be identical`, `Mismatched sub-array lengths`];
  let message = !AAClone.every(sub => Array.isArray(sub)) ? rules[0] : !AAClone.every(sub => sub.every(num => num + 0 === num)) ? rules[0] : !AAClone.every(sub => sub[0] + sub[1] === shared) ? rules[1] : !AAClone.every(sub => sub.length === 4) ? rules[2] : null;
  if (message !== null) {
    return message;
  }
  return AAClone.reduce((min, now, idx, AoA) => {
    let distTo0 = array => Math.abs(Math.floor(array[2]) + Math.floor(array[3]));
    min = distTo0(now) < distTo0(min) ? now : min;
    return min;
  });
};

/* Input rules:
1. Must be an array of arrays (only numbers)
2. Each sum of subArray[0] + subArray[1] must be identical
3. Each subArray.length = 4
*/
// AAa returns [ 3, 7, 3, -1 ] 
const AAa = [
  [5, 5, 1, -4],
  [3, 7, 3, -1],
  [7, 3, 4, 1],
  [5, 5, 5, 0]
];

// AA1 breaks rule 1
const AA1 = [
  [5, 5, 1, -4],
  [3, 7, 3, -1],
  [7, 3, ' X', 1],
  [5, 5, 5, 0]
];

// AAO breaks rule 1
const AAO = [
  [5, 5, 1, -4],
  [3, 7, 3, -1],
  [7, 3, 4, 1],
  [5, 5, 5, 0], {}
];

// AA2 breaks rule 2
const AA2 = [
  [5, 5, 1, -4],
  [3, 17, 3, -1],
  [7, 3, 4, 1],
  [5, 5, 5, 0]
];

// AA3 breaks rule 3
const AA3 = [
  [5, 5, 1, -4],
  [3, 7, 3, -1],
  [7, 3, 4, 1],
  [5, 5, 5]
];

console.log(crazyLogic(AAa));

console.log(crazyLogic(AA1));
console.log(crazyLogic(AAO));
console.log(crazyLogic(AA2));
console.log(crazyLogic(AA3));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-30
    • 2020-09-09
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 2013-03-30
    • 2021-02-24
    • 2011-08-03
    相关资源
    最近更新 更多