1

I have a data object with following contents:

{
  "content": {
    "id": "someID",
    "type": "unit",
    "method": "xyz",
    "blocks": [{
      "key": "blue",
      "data": [
        "Array"
      ]
    }, {
      "key": "red",
      "data": [
        "Array"
      ]
    }, {
      "key": "yellow",
      "data": [
        "Array"
      ]
    }, {
      "key": "black",
      "data": [
        "Array"
      ]
    }],
    "notes": "abc"
  }
}

I want to remove block that has key yellow, by looping over blocks, rest of the data should be preserved as is. So expected end result would be

{
  "content": {
    "id": "someID",
    "type": "unit",
    "method": "xyz",
    "blocks": [{
      "key": "blue",
      "data": [
        "Array"
      ]
    }, {
      "key": "red",
      "data": [
        "Array"
      ]
    }, {
      "key": "black",
      "data": [
        "Array"
      ]
    }],
    "notes": "abc"
  }
}

Data is dynamic so I dont know what would be returned, it might have a match for my condition or it might not.

I've tried a bunch of approaches but nothing seems to have worked so far. I can use lodash too if its any easier. None of those seems to be working. Any help/direction is appreciated

1. Using **delete**

const deleteUnwantedBlock = contentObj => {
  const updatedData = contentObj;
  const blocks = _.get(updatedData, 'blocks', []);

  blocks.forEach(block => {
    if (block.key.includes('yellow')) {
      delete updatedData.block;
    }
  });
  return updatedData;
};

console.log(deleteUnwantedBlock(data.content));```



2. Using rest operator:

    const deleteUnwantedBlock = contentObj => {
      const blocks = _.get(contentObj, 'blocks', []);
      blocks.forEach(block => {
        if (block.key.includes('yellow')) {
          let { block, ...restOfTheData } = updatedData;
        }
        return { ...updatedEntry };
      });
    };

    console.log(deleteUnwantedBlock(data.content));


3
  • 2
    Should be pretty straight forward with a filter data.content.blocks = data.content.blocks.filter(o=>o.key==='yellow'); Commented Feb 20, 2019 at 6:01
  • Possible duplicate of Javascript: How to filter object array based on attributes? Commented Feb 20, 2019 at 6:55
  • I dont see it as a duplicate, I didnt know I could use filter before posting my question Commented Feb 20, 2019 at 21:17

1 Answer 1

6

You just need to filter:

const obj = {
  "content": {
    "id": "someID",
    "type": "unit",
    "method": "xyz",
    "blocks": [{
      "key": "blue",
      "data": [
        "Array"
      ]
    }, {
      "key": "red",
      "data": [
        "Array"
      ]
    }, {
      "key": "yellow",
      "data": [
        "Array"
      ]
    }, {
      "key": "black",
      "data": [
        "Array"
      ]
    }],
    "notes": "abc"
  }
};
obj.content.blocks = obj.content.blocks.filter(({ key }) => key !== 'yellow');
console.log(obj);

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

3 Comments

Can I check something like .includes using filter? like if the key value is 'some thing yellow'
Sure, ({ key }) => key.includes('yellow')
Great that worked! I'll accept your answer when SO lets me to :)

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.