-1

I have an array of data similar to this structure:

const array = [
  {
    name: 'name',
    tags: [
      {
        name: 'xxxx',
        url: '/this',
      },
    ],
  },
  {
    name: 'name',
    tags: [
      {
        name: 'xxxxxxx',
        url: '/that',
      },
    ],
  },
];

I would like to return for example, the objects that contain only the url:'this'.

i tried to map the outer array first, and then using filter in the nested tag array, but without any luck. Any suggestions? thanks

Jay
  • 2,826
  • 2
  • 13
  • 28
  • 1
    do you want only the inner objects or only the outer or the outer with only the matching inner object? what have you tried? – Nina Scholz Dec 14 '22 at 12:16

1 Answers1

0

This works even when tags has multiple objects in its array

const arr = [{
  name: 'name',
  tags: [{
    name: 'xxxx',
    url: '/this'
  }]
}, {
  name: 'name',
  tags: [{
    name: 'xxxxxxx',
    url: '/that'
  }]
}]


console.log(arr.filter((item) => item.tags.some((tag) => tag.url === "/this")))
unhackit
  • 543
  • 3
  • 9