将缺少的数字插入数组

时间:2018-06-28 16:08:06

标签: javascript

我有以下两个数组,它们总是预先排序的:

[1,2,4,5,8]
[x,x,x,x,x]

我需要插入缺少的元素,并在相应的数组中放入y,以便输出为:

[1,2,3,4,5,6,7,8]
[x,x,y,x,x,y,y,x]

数据是单独到达的,但是它们总是匹配大小。

我已经尝试了以下方法,但是可以肯定的是我过于复杂了。

function action(numbers,data){
  var len=numbers.length;
  if (len<=1){
    return [numbers,data];
  }
  var new_data=[] //stores new data
  var new_number=[] //stores new numbers
  for(var i=1;i<len;i++){
    var diff=numbers[i] - numbers[i-1];

    if(diff>1){
      //there is gap here
      var val=0;
      diff--;

      for(var j=0;j<diff;j++){
        val=numbers[i-1] + j +1;
        new_number.push(val)
        new_data.push('y')
      }
      //put current info after missing data was inserted
      new_number.push(numbers[i])
      new_data.push(data[i])
    }
  }

  //adjust first entry
  new_number.unshift(numbers[0])
  new_data.unshift(data[0])

  return [new_number,new_data];
}

不一致,我自己也无法遵循。

action([2002,2005,2007],['x','x','x']) =>[2002,2003,2004,2005,2006,2007], [x,y,y,x,y,x]

但是以下是错误信息:

action([2002,2003,2007],['x','x','x']) =>[2002,2004,2005,2006,2007], [x,y,y,y,x]

输出应该是2002,2003,2004,2005,2006,2007和x,x,y,y,y,x

更新

在diff> 1中添加else似乎可以解决上述错误,但是解决方案一点也不优雅:

} else{
  new_number.push(numbers[i])
  new_data.push(data[i])
}

4 个答案:

答案 0 :(得分:2)

您可以使用while循环并从最后一个元素开始,然后首先完成。

var a = [1, 2, 4, 5, 8]
var b = ["x", "x", "x", "x", "x"];
var i = a[a.length - 1];

while (i-- > a[0]) {
  if (!a.includes(i)) {
    var index = a.indexOf(i + 1)
    a.splice(index, 0, i);
    b.splice(index, 0, "y");
  }
}

console.log(a);
console.log(b);

答案 1 :(得分:2)

你是如此亲密。我只需要移动几行。每次都需要将原始数字推送到新数组,而不仅仅是当差异太大时。

function tst(numbers, data){	
    var len=numbers.length;
    if (len<=1){
        return [numbers,data];
    }
    var new_data=[] //stores new data
    var new_number=[] //stores new numbers
    for(var i=1;i<len;i++){
        var diff=numbers[i] - numbers[i-1];

        if(diff>1){
            //there is gap here
            var val=0;
            diff--;

            for(var j=0;j<diff;j++){
                val=numbers[i-1] + j +1;
                new_number.push(val)
                new_data.push('y')
            }
        }
		
        //put current info after missing data was inserted
        new_number.push(numbers[i])
        new_data.push(data[i])
    }

    //adjust first entry
    new_number.unshift(numbers[0])
    new_data.unshift(data[0])

    return [new_number,new_data];
}
	
console.log(tst([1,2,4,5,8],['x','x','x','x','x']));

答案 2 :(得分:1)

您可以使用forEach:

const demo = (numbers,xy)=>{
  let expected = numbers[0],retNumbers=[],retXy=[];
  numbers.forEach(
    (current,index)=>{
      while(current>expected){
        retNumbers.push(expected);
        retXy.push("y");
        expected++
      }
      retNumbers.push(current);
      retXy.push(xy[index]);
      expected++;
    }
  );
  return [retNumbers,retXy];
}

console.log(demo(
  [1,2,4,5,8],
  ["a","b","c","d","e"]
))

答案 3 :(得分:0)

希望这会有所帮助。解决了虚拟数组。

var a = [1,4,6,7,8,12,14,55];
var b = [2,3,5,9,13,25,27,54,56];
b.forEach(function(value){
    var i = a.findIndex(function(oldvalue){return oldvalue>value;});
    i==-1 ? a.push(value) : a.splice(i,0,value);;
});
console.log(a)