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
            }
        ]
    }
]

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

2 Comments

I'd suggest keeping enforcing the callback on .filter of returning a boolean by return (a.campaignAssets.length > 0)
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
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);

2 Comments

Your solution will skip some elements because of not decreasing loop index after splice, refer: stackoverflow.com/q/24202962/5039495
thanks @Icycool , good catch. I updated my answer please check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.