Home > OS >  Remove data from my nested array of objects by matching values
Remove data from my nested array of objects by matching values

Time:06-14

Remove data from my nested array of objects by matching values. In my case I want to strip out the objects that are NOT active. So every object that contains active 0 needs to be removed.

[
    {
        "id" : 1,
        "title" : 'list of...',
        "goals": [
            {
                "id": 1569,
                "active": 0
            },
            {
                "id": 1570,
                "active": 1
            },
            {
                "id": 1571,
                "active": 0
            }
        ],
    },
    {
        "id" : 2,
        "title" : 'more goals',
        "goals": [
            {
                "id": 1069,
                "active": 0
            },
            {
                "id": 1070,
                "active": 1
            },
        ],
    },
]

The following will return the array in an unchanged status

public stripGoalsByInactiveGoals(clusters) {
    return clusters.filter(cluster =>
        cluster.goals.filter(goal => goal.active === 1)
    );
}

CodePudding user response:

array.filter wait a boolean to know if it has to filter data or not

in your case you have an array of array, you want to filter "sub" array by active goal

if you want to keep only active goals change your first filter by map to return a modify value of your array filtered by a condition

function stripGoalsByInactiveGoals(clusters) {
  return clusters.map(cluster => {
    return {
      goals: cluster.goals.filter(goal =>  goal.active)
    };
  });
}

var data = [{
    "goals": [{
        "id": 1569,
        "active": 0
      },
      {
        "id": 1570,
        "active": 1
      },
      {
        "id": 1571,
        "active": 0
      }
    ],
  },
  {
    "goals": [{
        "id": 1069,
        "active": 0
      },
      {
        "id": 1070,
        "active": 1
      },
    ],
  },
];

function stripGoalsByInactiveGoals(clusters) {
  return clusters.map(cluster => {
    return {
      goals: cluster.goals.filter(goal => goal.active)
    };
  });
}

console.log(stripGoalsByInactiveGoals(data));

CodePudding user response:

You can create another array (for the case when you need the input unchanged as well) and loop the input, appending each member objects' filtered goals array. You could also avoid appending the item if goals is empty after the filter, but this example doesn't do this, because it was not specified as a requirement.

let input = [
    {
        "goals": [
            {
                "id": 1569,
                "active": 0
            },
            {
                "id": 1570,
                "active": 1
            },
            {
                "id": 1571,
                "active": 0
            }
        ],
    },
    {
        "goals": [
            {
                "id": 1069,
                "active": 0
            },
            {
                "id": 1070,
                "active": 1
            },
        ],
    },
]

let output = [];
for (let item of input) {
    output.push({goals: item.goals.filter(element => (element.active))})
}

console.log(output);

CodePudding user response:

You can follow this for a dynamic approach:

 stripGoalsByInactiveGoals(clusters) {
    var res = [];

    this.data.forEach((item) => {
        let itemObj = {};
        Object.keys(item).forEach((key) => {
            itemObj[key] = item[key].filter(x => x.active != 0);
            res.push(itemObj);
        });
    });

    return res;
}

Stackbiltz Demo

  • Related