dannymac dannymac - 3 months ago 14
Javascript Question

How do you iterate over an array and extract elements based on their data type?

I'm trying to extract all of the objects from an array using native javascript. I keep getting one object as the value. What am I doing wrong?

var person1 = { name: "Daniel Machado", life: true };
var person2 = { name: "Marla Singer", life: null };

var array = ["stg", person1, 1, null, person2];


function extractedObj(objToExtract){
for(var i = array.length-1; i--;){
if (typeof array[i] === "object"){
splicedObj = array.splice(i, 1);
}
}
return splicedObj;
};

extractedObj(array);

// the result I'm looking for is:
// [Object, Object]
// [person1, person2]

// the result I'm getting is:
// [Object]
// [person1]


The output is only one object. I'm trying to pull them all out. Ideally, I'd like to do this using forEach and/or arrow functions, but anything works. Not really sure why you have to decrement for splice in this case either. Have never really understood that, just know that's what many examples show. If someone could explain that it'd be appreciated.

Answer

You're overwriting splicedObject each time through the loop. You need to push the item you're removing onto an array of results.

function extractedObj(objToExtract){
  var splicedObj = [];
  for(var i = array.length-1; i--;){
    if (typeof array[i] === "object"){
      splicedObj.push(array[i]);
      array.splice(i, 1);
    }
  }
  return splicedObj;
};

The reason you need to decrement when splicing is because it adjusts the indexes of all the elements after the one you remove. If you go through the array in the forward direction, then after you remove element 2, the element that used to be 3 becomes 2. The next time through the loop, you will increment i to 3, so you'll never check the item that took the place of the old 2.

By going the other way, the elements that get renumbered are just the ones that have already been tested, so you won't miss anything.

Comments