-1

I have this kind of json.

let arr1 = [
    {
        "packageReference": "1234",
        "displayName": "Business",
        "description": "Includes...",
        "promotion": {
          "packageReference": "1234",
          "displayName": "$100 Standard",
          "optionGroup": [
            {
              "displayName": "Access",
            },
            {
              "displayName": "Contract"
            },
            {
              "displayName": "Equipment"
            },
            {
              "displayName": "Features"
            },
            {
              "displayName": "Fees",
            }
          ]
        }
      }
]

I need to remove only the object in the arr1[0].promotion.optionGroup where the displayName is 'Fees' and to return the new object without him.

2

2 Answers 2

2

You could do it by filtering the sub array like so:

let arr1 = [
    {
        "packageReference": "1234",
        "displayName": "Business",
        "description": "Includes...",
        "promotion": {
          "packageReference": "1234",
          "displayName": "$100 Standard",
          "optionGroup": [
            {
              "displayName": "Access",
            },
            {
              "displayName": "Contract"
            },
            {
              "displayName": "Equipment"
            },
            {
              "displayName": "Features"
            },
            {
              "displayName": "Fees",
            }
          ]
        }
      }
];

arr1 = arr1.map(e => {
  e['promotion']['optionGroup'] = 
       e['promotion']['optionGroup'].filter(s => s['displayName'] != 'Fees');
  return e;
});


console.log(arr1);

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

Comments

1
// Get new array without the Fees one
const newGroup = arr1[0].promotion.optionGroup.filter(group => group.displayName !== 'Fees');

// Put new group into the object
arr1[0].promotion.optionGroup = newGroup;

Could also do it without creating a variable, but added it for cleanness.

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.