0

Good evening

Wondering if anyone could give me hand, I seem to be going in circles I am trying to get all the objects containing the key/field "type" from an array, by another array of strings.

{
  "someData": [
    {
      "date": "someDate",
      "someNestedData": [
        {
          "id": "1",
          "type": "type1",
          "dateCreated": "someDate"
        },
        {
          "id": "2",
          "type": "type2",
          "dateCreated": "someDate"
        }
      ]
    },
      {
      "date": "someDate",
      "someNestedData": [
        {
          "id": "3",
          "type": "type1",
          "dateCreated": "someDate"
        },
        {
          "id": "4",
          "type": "type4",
          "dateCreated": "someDate"
        }
      ]
    }

  ]
}

and another array of strings const types = ['type1', 'type2]` I am trying to filter out the array so that any object not containing the key in the types array is filtered out.

So the end result would be

{
  "someData": [
    {
      "date": "someDate",
      "someNestedData": [
        {
          "id": "1",
          "type": "type1",
          "dateCreated": "someDate"
        },
        {
          "id": "2",
          "type": "type2",
          "dateCreated": "someDate"
        }
      ]
    },
      {
      "date": "someDate",
      "someNestedData": [
        {
          "id": "3",
          "type": "type1",
          "dateCreated": "someDate"
        },
      ]
    }

  ]
}

I have tried something along the lines of

data.someData.someNestedData.indexOf(function (i) {
  return types.indexOf(i.type);
})

as well as a few other bloated approaches, just not coming right and looking for something quite performant. TIA :)

1 Answer 1

1

You can do something like this:

const types = ['type1', 'type2'];
const res = [
    {
      "date": "someDate",
      "someNestedData": [
        {
          "id": "1",
          "type": "type1",
          "dateCreated": "someDate"
        },
        {
          "id": "2",
          "type": "type2",
          "dateCreated": "someDate"
        }
      ]
    },
      {
      "date": "someDate",
      "someNestedData": [
        {
          "id": "3",
          "type": "type1",
          "dateCreated": "someDate"
        },
        {
          "id": "4",
          "type": "type4",
          "dateCreated": "someDate"
        }
      ]
    }
]
// map over the array
.map(obj => ({
     // rebuild the object 
     ...obj, 
     // but replace "someNestedData" with the list of element that has the 'type' filed included in types
     someNestedData: obj.someNestedData.filter(
          el => types.includes(el.type)
     )
}))
console.log(res)

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

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.