Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter a nested array object data

I am having this array of object

 const myArrayData = [
    {
        "user": "564b6deec50de8d827c0a51a",
        "images": [
            {
                "image": "https://link-to-image.com.iga",
                "size": 16
            },
            {
                "image": "https://3link-to-image.com.iga2",
                "size": 70
            },
        ],
    },
    {
        "user": "564b7527ecc479e4259edff7",
        "images": [
            {
                "image": "https://44link-to-image.com.iga",
                "size": 32
            },
            {
                "image": "https://13link-to-image.com.iga23",
                "size": 45
            },
        ],
    },
    {
        "user": "564e0a18c8cd4b5420a9250c",
        "images": [
            {
                "image": "https://84link-to-image.com.iga",
                "size": 120           
             },
            {
                "image": "https://93link-to-image.com.iga23",
                "size": 8
            },
        ],
    },
]

I want to filter out the images array object that has a size below 20

So I should get this response which filtered out the images array object that has a size below 20

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    {
        "user": "564b6deec50de8d827c0a51a",
        "images": [
            {
                "image": "https://3link-to-image.com.iga2",
                "size": 70
            },
        ],
    },
    {
        "user": "564b7527ecc479e4259edff7",
        "images": [
            {
                "image": "https://44link-to-image.com.iga",
                "size": 32
            },
            {
                "image": "https://13link-to-image.com.iga23",
                "size": 45
            },
        ],
    },
    {
        "user": "564e0a18c8cd4b5420a9250c",
        "images": [
            {
                "image": "https://84link-to-image.com.iga",
                "size": 120           
             }
        ],
    },
  ]

How can I achieve this?

>Solution :

hey you should try this

const result = myArrayData.map(data => {
  return {
      user: data.user,
      images: data.images.filter(item =>
        item.size >= 20
      ),
  }
})

code

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading