0

Javascript

I have a nested array of objects, I'm trying to filter the given array of objects using a property from the third level of its array property value. For example, from the below array I like to filter the entire array using the property ListId: 10

Example

let test = {
   "test":true,
   "group":[
      {
         "name":"header",
         "value":[
            {
               "id":"0",
               "list":[
                  {
                     "ListId":10,
                     "name":"string1",
                     "state":"BY",
                     "techId":0
                  },
                  {
                     "ListId":11,
                     "name":"string2",
                     "state":"BY"
                  },
                  {
                     "ListId":12,
                     "name":"string3",
                     "state":"BY"
                  }
               ]
            }
         ]
      },
      {
         "name":"header2",
         "value":[
            {
               "id":"01",
               "list":[
                  {
                     "ListId":100,
                     "name":"string1",
                     "state":"BY",
                     "techId":0
                  },
                  {
                     "ListId":111,
                     "name":"string2",
                     "state":"BY"
                  },
                  {
                     "ListId":121,
                     "name":"string3",
                     "state":"BY"
                  }
               ]
            }
         ]
      }
   ]
}

Filtervalue with ListId = 10

Expected output :

{
   "test":true,
   "group":[
      {
         "name":"header",
         "value":[
            {
               "id":"0",
               "list":[
                  {
                     "ListId":10,
                     "name":"string1",
                     "state":"BY",
                     "techId":0
                  }
               ]
            }
         ]
      }
   ]
}

How can I use the filter method using javascript to get this expected result?

4
  • If the objects outer shell doesn't change you can go about it like this z.group1.value[0].list[0] but if the structure changes, then I'm not quite sure. Commented May 24, 2022 at 5:52
  • So many things unclear here... By entire array, do you mean array of z.group1.value[0].list? Is z.group1.value always going to have exactly one entry everytime? Commented May 24, 2022 at 5:54
  • @lakshman It would be super helpful, if you could add more details here, a few helpful questions to answer would be, 1. Do you know the structure of your data, is it fixed or may change. 2. If you need a generic solution, is there a limit on data depth. Commented May 24, 2022 at 6:03
  • Hi, Sorry all, I just changed the expected result output for that query. Commented May 24, 2022 at 7:17

2 Answers 2

1

You can two it in two times :

  • First, filter the list arrays,
  • Secondly filter the groups array using the some method

let test= {
  "test": true,
  "group": [
    {
      "name": "header",
      "value": [
        {
          "id": "0",
          "list": [
            {
              "ListId": 10,
              "name": "string1",
              "state": "BY",
              "techId": 0
            },
            {
              "ListId": 11,
              "name": "string2",
              "state": "BY"
            },
             {
              "ListId": 12,
              "name": "string3",
              "state": "BY"
            }
          ]
        }
      ]
    },
    {
      "name": "header2",
      "value": [
        {
          "id": "01",
          "list": [
            {
              "ListId": 100,
              "name": "string1",
              "state": "BY",
              "techId": 0
            },
            {
              "ListId": 111,
              "name": "string2",
              "state": "BY"
            },
             {
              "ListId": 121,
              "name": "string3",
              "state": "BY"
            }
          ]
        }
      ]
    }
  ]
}

test.group.forEach(group => {
  group.value.forEach(value => {
    value.list = value.list.filter(list => list.ListId === 10)
  })
})

test.group = test.group.filter(group => group.value.some(value => value.list.length > 0))

console.log(test)

Note : You should use plural names for you arrays, it helps understanding the data. For example lists not list for the array.

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

Comments

0

let z ={"group1": [
  {
    "name": "header",
    "value": [
      {
        "id": 0,
        "list": [
          {
            "ListId": 10,
            "Name": "string1"
          },
          {
            "ListId": 11,
            "Name": "string2"
          }
        ]
      }
    ]
  }
]}


// This function was written from understading that 'group1' is not a fixed property, but part of a dynamic list due to the number '1'
const getItemByListId = (list, listId) => {
  const listKeys = Object.keys(list);
  const selectedListKey = listKeys.find(key => {
    const groupItems = list[key];
    const selectedItem = groupItems.find(({ value: nestedItems }) => {
      const selectedNestedItem = nestedItems.find(({ list }) => {
        const selectedList = list.find(({ ListId }) => ListId === listId)
        return selectedList;
      });
      return selectedNestedItem;
    });

    return selectedItem;
  });
  
  if (!selectedListKey) {
    return null;
  }

  return list[selectedListKey];
};


console.log(getItemByListId(z, 10));

1 Comment

Thanks @Ron for the solution, but my expected result would be. { "group1": [ { "name": "header", "value": [ { "id": 0, "list": [ { "ListId": 10, "Name": "string1" } ] } ] } ] }

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.