-2

i've an array

const exampleArray = [
    {
     id: 1,
     name: test
    },
    {
     id: 1,
     name: test
    },
    {
    },
]

now the array exampleArray live inside another array of objects so

const exampleNest = [{
    property1: {},
    property2: [],
    exampleArray: [{...}],
}]

I need to remove the empty object inside exampleArray and return the rest of the array.

I already tried to use filter

const noEmptyObjects = exampleNest.filter(({ exampleArray }) =>
    exampleArray.filter(attachment => attachment !== {}),
  );

i tried too Object.keys(attachment).length !== 0 instead of attachment !== {} but i continue to receive the array with the empty item.

Legeo
  • 784
  • 4
  • 20
  • 44

3 Answers3

1

Your can use simple https://stackoverflow.com/a/32108184/3932166 to check empty object and for array with this one.

exampleArray.filter(attachment => (condition from above link));
Nitish
  • 995
  • 7
  • 17
0

You can filter your array by checking if the object has an id property

exampleArray.filter(function(obj) { return obj.hasOwnProperty("id"); })

AlbertVanHalen
  • 632
  • 4
  • 12
0

Because of {} is object. So, {} == {} always false.

exampleArray.filter(attachment => JSON.stringify(attachment) !== "{}")
seunggabi
  • 1,699
  • 12
  • 12