0

Hi How to check array inside an array length is empty then remove the parent array from the main array ,

think about i have an array

[
    {
        "id": 71,
        "campaignAssets": [
            {
                "id": 128
            }
        ]
    },
    {
        "id": 99,
        "campaignAssets": []
    }
]

from above array id:71 have campaignAssets array which is length is 1 but on the other one "id": 99 dont have the campaignAssets so i have to remove the parent array which means

    {
        "id": 99,
        "campaignAssets": []
    }

so final array should be

[
    {
        "id": 71,
        "campaignAssets": [
            {
                "id": 128
            }
        ]
    }
]
amu
  • 778
  • 6
  • 16
Gayathri Mohan
  • 2,924
  • 4
  • 19
  • 25

2 Answers2

6

This proposal features two solutions,

  1. generate a new array and assign it to the original array
  2. delete unwanted items without generating a temporary.

1. With a new array

You could filter it with Array#filter

var data = [{ "id": 71, "campaignAssets": [{ "id": 128 }] }, { "id": 99, "campaignAssets": [] }];

data = data.filter(function (a) { return a.campaignAssets.length; });

console.log(data);

In ES6 it's even shorter

var data = [{ "id": 71, "campaignAssets": [{ "id": 128 }] }, { "id": 99, "campaignAssets": [] }];

data = data.filter(a => a.campaignAssets.length);

console.log(data);

2. Without a new array

For a in situ solution, keeping the array and delete only the elements with zero length, I suggest to use backward iteration and check the length and use Array#splice accordingly.

var data = [{ "id": 71, "campaignAssets": [{ "id": 128 }] }, { "id": 99, "campaignAssets": [] }],
    i = data.length;

while (i--) {
    if (!data[i].campaignAssets.length) {
        data.splice(i, 1);
    }
}

console.log(data);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • I'd suggest keeping enforcing the callback on `.filter` of returning a boolean by `return (a.campaignAssets.length > 0)` – Icycool Jun 28 '16 at 10:04
  • the callback itself uses a truthy/falsey value. *and constructs a new array of all the values for which callback returns a value that coerces to true.* [source](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – Nina Scholz Jun 28 '16 at 10:08
0

var data =[
    {
        "id": 71,
        "campaignAssets": [
            {
                "id": 128
            }
        ]
    },
    {
        "id": 99,
        "campaignAssets": []
    }
]
var i = data.length;
while (i--) {
    if(data[i].hasOwnProperty('campaignAssets') && data[i]['campaignAssets'].length==0)
       data.splice(i, 1)
}

// This one is wrong implementation as pointed out, it will not delete element from reducing array..
// data.forEach(function(parent,index){
//  if(parent.hasOwnProperty('campaignAssets') && parent['campaignAssets'].length==0)
//       data.splice(index, 1)
// });

console.log(data);
A.T.
  • 24,694
  • 8
  • 47
  • 65
  • Your solution will skip some elements because of not decreasing loop index after splice, refer: http://stackoverflow.com/q/24202962/5039495 – Icycool Jun 28 '16 at 10:06
  • thanks @Icycool , good catch. I updated my answer please check – A.T. Jun 28 '16 at 11:28