4

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

1
  • 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 :)). Commented Feb 20, 2014 at 8:58

5 Answers 5

4

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]
3
  • 1
    You could also do a.filter(isFinite) if it's just numbers. Commented Feb 20, 2014 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 [] Commented Feb 20, 2014 at 8:52
  • 1
    Yes I guess it has a quite specific use. Btw, .filter(Number) will filter out zeroes while isFinite won't. Commented Feb 20, 2014 at 9:08
1

The filter function is what you are after.

var newArray = a.filter(function(item) {
  return item !== undefined;
});
1
  • 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 Commented Feb 20, 2014 at 8:50
0

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=[].

0

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
}
0

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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.