Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Array length is 7

Original array

var arr = [2, 4, 6];

Needed array

arr = [null,null,2,null,4,null,6];
  • 0 is not present in array so need to replace with null,
  • 1 is not available replace with null and
  • 2 is available so put 2 in new array so on..
share|improve this question
    
What problem are you really trying to solve? Because it appears that your data is a poor structure for whatever problem you have. If you describe the actual problem you're trying to solve, we can perhaps help you find a better type of data structure rather than converting one inefficient data structure to another inefficient one. –  jfriend00 37 mins ago
    
Iam using this in selecting week days array for sun -0 , mon-1, tue-2 like this. user can select one or more days in a week. iam pushing select days index in to array. need to push null value if days are not selected. –  pavan kumar devu 2 mins ago

10 Answers 10

You can use the splice() method on the 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 modifies the original array.

share|improve this answer

Given you have the only input arr which you want to fill null inside. Try this:

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];
share|improve this answer

You just need to find the max value in the array and then iterate from 0 to that max, checking each value to see if it was 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 asked in my comments, I'd like to know what problem you're actually trying to solve because it seems like both the original and the newly created data structures are inefficient structures that could probably use different form of data and work more efficiently. But, we can only help you make a wiser choice if you explain the actual problem, rather just your attempted solution.

share|improve this answer

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))

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

Interesting quiz:

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 applies to array consists of random number of sorted integers.

share|improve this answer
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);
share|improve this answer

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 ]

share|improve this answer

I will write a permanence case for all 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]
share|improve this answer

check it...

var arr = new Array(7);

arr[0] = "null";
arr[1] = "null";
arr[2] = 2;
arr[3] = "null";
arr[4] = 4;
arr[5] = "null";
arr[6] = 6;

document.write(arr.toString());
share|improve this answer
    
Try to provide solution which is dynamic in nature.. Solution which can work for n size array or so. - iAmNotTheOneWhoDownVoted –  Sunil B N 18 mins ago
    
This is not a solution. This wont work for the problem the OP has posted. The downvote is by me. –  Clyde Lobo 12 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.