Java Script - add null value to array if element is not present at specific index

Array length is 7

Original array

var arr = [2, 4, 6];

      

Required array

arr = [null,null,2,null,4,null,6];

      

  • 0 is not in the array, so you need to replace it with a zero value,
  • 1 cannot be replaced with zero and
  • 2 is available so to put 2 into a new array, and so on.
+3


source to share


9 replies


I'll write longevity for all the answers soon.



function createArrayFromArray(array, length) {

var new_array = new Array(length);

   for (var i = 0; i < new_array.length; i++) {
      new_array[i] = null;
   }

  for (var i = 0; i < array.length; i++) {
      new_array[array[i]] = array[i];
   }

   return new_array;
}

console.log(createArrayFromArray(arr, 7)); //[null, null, 2, null, 4, null, 6]

      

+1


source


You can use method splice()

for array

var arr=[2,4,6];
var l = arr[arr.length-1];
for(var i=0; i<=l; i++){
    if(arr[i] !== i){
        arr.splice(i, 0, null);
    }
}

      



Output: [null, null, 2, null, 4, null, 6]

This changes the original array.

+3


source


You just have to find the maximum value in the array and then loop over from 0 to that max value, checking each value to see if it is present in the source or not:

var arr = [2, 4, 6];

var max = Math.max.apply(Math, arr);
var result = [];
for (var i = 0; i <= max; i++) {
    if (arr.indexOf(i) !== -1) {
        result[i] = i;
    } else {
        result[i] = null;
    }
}

      

Working demo: http://jsfiddle.net/jfriend00/c7p8mkqy/


As I said in my comments, I would like to know what problem you are trying to solve, because it seems that both the original and the newly created data structure are inefficient structures that can probably use different forms of data and work more efficiently. ... But we can only help you make smarter choices if you explain the actual problem, not just your solution.

+1


source


Considering that you have only one input arr

that you want to fill inside. Try the following:

var arr = [2, 4, 6];

var output = [];
while (arr.length>0){
    var first = arr.splice(0,1);
    while (output.length<first[0])
        output.push(null);
    output.push(first[0]);
}

// output should be [null,null,2,null,4,null,6];

      

0


source


Try:

var arr = [2, 4, 6];
var new_arr = [];
var i = 0;

while(i < 7){
	var pos = arr.indexOf(i++);
	new_arr.push(pos !== -1 ? arr[pos] : null)
}

 document.write(JSON.stringify(new_arr, null, 4))
      

Run codeHide result


0


source


var arr = [2, 4, 6];
var result = new Array(7);
arr.forEach(function(a) { result[a] = a;});

      

0


source


Try splice ():

var arr = [2, 4, 6];
var i = 0,
  l = arr[arr.length - 1];
while (i < l) {
  if(i !== arr[i])
    arr.splice(i, 0, null);
  i++;
}
console.log(arr); //[ null, null, 2, null, 4, null, 6 ]
      

Run codeHide result


0


source


Interesting poll:

var arr = [2, 4, 6]
var n = 0

while(arr.length > n) {
  if(arr[n] !== n) {
    arr = arr.slice(0,n).concat(null, arr.slice(n))
  }
  n++
}

console.log(arr) // [null, null, 2, null, 4, null, 6]

      

This approach is applied to an array consisting of a random number of sorted integers.

0


source


var arr = [2, 4, 6];
var narr = (new Array(arr.sort()[arr.length-1]))
arr.map(function(v){
    narr[v] = v;
}); 
for (var i = 0; i<narr.length; i++) narr[i]||(narr[i]=null);
console.log(narr);

      

0


source







All Articles