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


user988544
  • 556
  • 1
  • 11
  • 32
  • 2
    Should be pretty straight forward with a filter `data.content.blocks = data.content.blocks.filter(o=>o.key==='yellow');` – Imran Feb 20 '19 at 06:01
  • Possible duplicate of [Javascript: How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes) – adiga Feb 20 '19 at 06:55
  • I dont see it as a duplicate, I didnt know I could use filter before posting my question – user988544 Feb 20 '19 at 21:17

1 Answers1

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);
Snow
  • 3,820
  • 3
  • 13
  • 39