1

I am using this code to iterate over an array and delete the element if it contains no itemsToPost.

  rssFeeds.forEach(function(feed, index, array){
    if(feed.itemsToPost.length == 0){
      delete rssFeeds[index]
    }
  });

However, the array that I receive from this, assuming that all elements are deleted looks like:

[ ,  ]

And when I run this against Lodash's method to test if an array is empty.

console.log(_.isEmpty(array))) // returns 'false'

How can I completely delete these elements such that the array is truly empty?

Update:

I've found a solution using Array.prototype.filter as was suggested, such as:

  function hasNewItems(array) {
    return array.itemsToPost.length > 0;
  }

  var newArray = rssFeeds.filter(hasNewItems)
  console.log(newArray); // returns []

It isn't necessarily deleting the array but it has the desired effect.

Anthony
  • 10,564
  • 12
  • 47
  • 65

1 Answers1

2

delete is not the correct way to remove element from array, you should use rssFeeds.splice(index, 1) instead.

Lye Fish
  • 2,258
  • 12
  • 20