Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找左右元素的索引';Javascript中数组中的空索引_Javascript_Arrays_Algorithm_Binary Search - Fatal编程技术网

查找左右元素的索引';Javascript中数组中的空索引

查找左右元素的索引';Javascript中数组中的空索引,javascript,arrays,algorithm,binary-search,Javascript,Arrays,Algorithm,Binary Search,我尝试使用简单的线性插值来替换数据数组中的任何空值。 为了得到线性插值计算,我需要找到左右两个值。 我已经把这个编码如下 const data = [null, null, 537309, 514619, 548638, 618428, 615091, 695349, 828929, 925076, 741585, 650574, 730432, 795618, 997527, 1112017, 1137697, 1231562, 1210135, 1204056, 1252409, 12612

我尝试使用简单的线性插值来替换数据数组中的任何空值。 为了得到线性插值计算,我需要找到左右两个值。 我已经把这个编码如下

const data = [null, null, 537309, 514619, 548638, 618428, 615091, 695349, 828929, 925076, 741585, 650574, 730432, 795618, 997527, 1112017, 1137697, 1231562, 1210135, 1204056, 1252409, 1261298, 1230198, 1336902, 1420173, 1502653, 1553030, 1514224, 1397548, 1353077, 1336532, 1250078, 1274630, 1231772, 1295328, 1249025, null, 1394515, null, null];

let result = [];
let i=1;
let j=1;
let before;
let after;

data.forEach((elem,index)=>{
    if(elem === null || typeof elem !=="number"){
         const condition = !Object.is(null, data3[before]) && !Object.is(null, data3[after])
         do {
         i = i +1;
         j = j +1;
         after = index + j;
         before = index - i;
         result.push({before:before, index:index,after:after})
         } while (!condition);
  }
})
console.log(result)
[
  { before: -2, index: 0, after: 2 },
  { before: -2, index: 1, after: 4 },
  { before: 32, index: 36, after: 40 },
  { before: 33, index: 38, after: 43 },
  { before: 33, index: 39, after: 45 }
]
如您所见,如果我将一些元素放回数据,它们将返回未定义的元素。 例如
数据[-2]
数据[43]
数据[45]
将返回
未定义的
。
但我将递归调用整个函数,这样每个null都将被以前计算的输出所取代

预期输出应为数据范围内距空索引最近的两个索引:

[
  { before: 2, index: 0, after: 3 },
  { before: 0, index: 1, after: 2 },
  { before: 35, index: 36, after: 37 },
  { before: 36, index: 38, after: 37 },
  { before: 37, index: 39, after: 38 }
]
这里可以测试线性插值 以及来自


请提供预期输出。对于数组最末端的
null
值,您希望发生什么?在这里你不能使用插值…你还需要在我们有几个连续的空值的情况下指定预期的行为(因为我们不希望所有这些值都具有相同的值,对吗?)我刚刚编辑了这篇文章,如果我回答不正确,请留下任何问题。我只是想确保如果我的数据数组中有空数据,我想用简单的线性插值替换这些空数据。如果这些空值不能替换为它,那么此时只需将其保持为空值即可。
const points = [{ x: 1, y: 1}, { x: 2, y: 2 }];
const calculate = linearInterpolation(points);

calculate({ x: 1.5 }); // y -> 1.5
calculate({ y: 1.5 }); // x -> 1.5