1

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

3 Answers 3

4

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

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

7 Comments

Your result is missing the goals object. You should return the original object inside the map function, with just the goals property modified.
yes thank you i edit my reply to have the correct goal object return
Please don't compare active with 1. We do not know whether an active item is necessarily 1. We only know that the active elements are not 0.
yes thank you i edit my reply with I also change the condition to cluster.goals.filter(goal => goal.active)
@3limin4t0r I do not see any hint in the question that would suggest that an active value is necessary 1. I only see "So every object that contains active 0 needs to be removed" which means that a value of 0 is necessarily not active. This is why I had a check of !== 0 initially in my answer, but later I thought that any falsy value seems to be inactive and edited my answer accordingly. What if an item has an active of 2? Should we remove it? I don't think so.
|
0

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

Comments

0

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

Comments

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.