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.

I am having an array in which i have added 3 data in different index, as below

  var a=[];
   a[1]=4;
   a[3]=7;
   a[4]=8;

now my array will look like this

 a=[undefined, 4, undefined, 7, 8, undefined]

i want to take the value alone from the array and need to add in another array. is there any simplest way, that i can make use of it. Till now i am taking the value using "for" loop, it is ok when i have small number of data. here i need only 3 values, but the loop execute for 6 time.

Thanks in advance

share|improve this question
    
On a side note - sparse arrays are extremely slow compared to whole arrays. Array 'holes' cause JS engines to vastly degrade in performance and causes very bad "de-optimizes". Full arrays on the other hand are almost as fast as C arrays. If you explain the use case (why you got those holes in the array) we can probably give you a much better guided answer. I'm yet to see a justified use case for them (I might be surprised today :)). –  Benjamin Gruenbaum Feb 20 at 8:58

5 Answers 5

up vote 1 down vote accepted

The filter function is what you are after.

var newArray = a.filter(function(item) {
  return item !== undefined;
});
share|improve this answer
1  
Note, checking typeof against undefined is not required when you have an undefined, but declared variable. The use of typeof var !== 'undefined' is for when the identifier doesn't exist. Function arguments are automatically declared and assigned 'undefined' so you can do item !== undefined –  Benjamin Gruenbaum Feb 20 at 8:50
    
@BenjaminGruenbaum didn't know that. I will update my answer. Thanks :) –  Jim Jeffries Feb 20 at 8:52

Most array methods skip over "holes" anyway.

If you don't explicitly want to filter them out, just call an array method on them:

var arr = a.filter(function(){return true});

Or shorthand:

[,4,,7,8].filter(function(){ return true}); // [4, 7, 8]
share|improve this answer
1  
You could also do a.filter(isFinite) if it's just numbers. –  elclanrs Feb 20 at 8:50
    
@elclanrs I considered it (or map(Number)) to be honest :) The only issue I saw with that is array values that are not numbers, or array values that are declared but set to undefined. For example with holes - [undefined] should go to [undefined] while [,,] should go to [] –  Benjamin Gruenbaum Feb 20 at 8:52
1  
Yes I guess it has a quite specific use. Btw, .filter(Number) will filter out zeroes while isFinite won't. –  elclanrs Feb 20 at 9:08

You could consider a 'key/value' object instead of an array.

> var a={}; 
> a[1]=4; 
> a[3]=7; 
> a[4]=8;

Note that the difference is the way you declare 'a': a={} rather than a=[].

share|improve this answer

The point is, whenever you set an index in an array whereas the other previous indexes are not there, your array add them with undefined values. If you have a huge amount of data in this array it is not a good practice.

You can use filter to actually filter the undefined values but the point is, when the array has a huge amount of data, what you do is creating 2 array which the first array has lots of indexes with undefined values, which is not a good practice. I believe it is more of a HashMap instead of Array.

if I where you I would use javascript object instead, and to loop through it like an array, you can do:

var a={};
a[1]=4;
a[3]=7;
a[4]=8;
var keys = Object.keys(a);
for(var i=0;i<keys.length;i++){
    var value = a[keys[i]];
    //do whatever you want
}
share|improve this answer

The raw array:

var originalArr = [];
a[1] = 4;
a[3] = 7;
a[4] = 8;

The filter logic:

function filterUndefined(originalArr){
    var valuesArray = [];
    for(var i = 0; i < originalArr.length; i++){
        if(originalArr[i] !== undefined){
          valuesArray.push(a[i]);
        }
        return valuesArray;
    }
}

Result:

[4,7,8]
share|improve this answer

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.