Filtering an array of objects The code does the following:


Gets an array of objects and filters them according to some properties
Returns the array of objects with the filtered data


I'm using the following code which is working and I wonder if there is a way to write it better.

By the way, I can use LodashJS if it's helpful here.

filterNullUndefinedOrRejected: function(oData) {
    if (oData &amp;&amp; oData.length &gt; 0) {
        var aNewExtendedObjects = [];
        for (var i = 0; i &lt; oData.length; i++) {
            if (oData[i].va === null || oData[i].val === undefined || oData[i].state === "rejected") { 
                if (oData[i].state === "rejected") {
                      console.log(oData[i].error);
                }        
                oData.splice(i, 1);
                i--;
            }
            else{
                aNewExtendedObjects.push(oData[i].val);
            }
        }
        return aNewExtendedObjects[0];  
    }
    return oData;   
},
splice mutates the existing array. You wouldn't want to do that as you might not be sure where else the array is being used. You might be expecting values in the array after calling this function, yet this function wiped them off.

Now if you're really sure you want to use splice to remove items in an array, loop backwards. The ending i-- isn't very visible and missing it would mean skipped items.

Lodash isn't necessary as you have the native array.filter. This is unless you still plan to support browsers that don't do ES5 (like IE8 and older).

Your function isn't consistent. The ideal return is an array. But if I put in a falsy value, it returns that value, not an array.

The length check is meaningless. An empty array has the length of 0 which means the loop will never run at all.

Your function is essentially just including all values that aren't null, undefined or have a state of rejected. However, the name is misleading. You never explicitly mention that you're only getting the val properties as a result. You should name your function explicitly, or split off the operations separately.

As noted in the comments (and it wasn't apparent in the code), you're collecting the val property of the filtered items. This can easily be done with an array.map. So after filtering down to the items we do need, we transform the values to the ones we need.

filterNullUndefinedOrRejected: function(oData){
  return !oData ? [] : oData.filter(function(oDatum){
    if(oDatum.state === 'rejected') console.log(oDatum.error);
    return oDatum.val !== null &amp;&amp; oDatum.val !== undefined &amp;&amp; oDatum.state !== "rejected";
  }).map(function(oData){
    return oData.val;
  });
}