-3
{
  "view_list": [
    {
      "id": "1"
    },
    {
      "id": "2"
    },
    {
      "id": "3"
    },
    [
      {
        "id": "4"
      },
      {
        "id": "5"
      }
    ],
    {
      "id": "6"
    },
    [
      {
        "id": "7"
      },
      {
        "id": "8"
      }
    ],
    {
      "id": "9"
    },
    {
      "id": "10"
    },
    {
      "id": "11"
    }
  ]
}

The above JSON i want filter only id="8" .Actually i try filter method in JavaScript not working.

I filter the array inside array. I used filter method, the method not searched inside array. Give the solution any one.

10
  • It's not clear what you want, please provide an example of the input and output that you would want and tell us what you've tried. We can help you fix your code, but we're not a code-producing machine. Commented Feb 1, 2023 at 6:46
  • i try search for inside array Commented Feb 1, 2023 at 6:47
  • only i filter id="8" value Commented Feb 1, 2023 at 6:48
  • How to delete id without flatmap? Commented Feb 1, 2023 at 7:56
  • What do you mean by delete id? Commented Feb 1, 2023 at 8:27

4 Answers 4

1

you can simply use flat befor find or filter by id

like this :

data.view_list.flat().find(view=>view.id == 8) 

let data = {
  "view_list": [
    {
      "id": "1"
    },
    {
      "id": "2"
    },
    {
      "id": "3"
    },
    [
      {
        "id": "4"
      },
      {
        "id": "5"
      }
    ],
    {
      "id": "6"
    },
    [
      {
        "id": "7"
      },
      {
        "id": "8"
      }
    ],
    {
      "id": "9"
    },
    {
      "id": "10"
    },
    {
      "id": "11"
    }
  ]
}

console.log(data.view_list.flat().find(view=>view.id == 8))

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

1 Comment

How to delete id without flatmap?
1

You can use Array.flatMap method and then filter the array to achieve what you want like below:

const source = {
  "view_list": [{
      "id": "1"
    },
    {
      "id": "2"
    },
    {
      "id": "3"
    },
    [{
        "id": "4"
      },
      {
        "id": "5"
      }
    ],
    {
      "id": "6"
    },
    [{
        "id": "7"
      },
      {
        "id": "8"
      }
    ],
    {
      "id": "9"
    },
    {
      "id": "10"
    },
    {
      "id": "11"
    }
  ]
}

const target = source.view_list.flatMap(r => r).filter(r => r.id === "8")
console.log(target)

const alternate = source.view_list.filter(r =>
  Array.isArray(r) || r.id !== "8" // skip the arrays
).map(r => {
  if (Array.isArray(r)) { // filter the arrays here
    const filtered = r.filter(q=> q.id !== "8")
    return filtered.length ? filtered : null // if it has no elements set it to null
  }
  return r
}).filter(r => r !== null); // filter out null values

console.log(alternate)

Edit As the question evolves.

2 Comments

How to delete id without flatmap?
Thanks eldar nice..
0

You can use a conbination of flat() and filter(), like this:

view_list.flat().filter(d=> d.id ==="8")

Comments

0

one way is to flat a.view_list

const a = {
  view_list: [
    {
      id: '1'
    },
    {
      id: '2'
    },
    {
      id: '3'
    },
    [
      {
        id: '4'
      },
      {
        id: '5'
      }
    ],
    {
      id: '6'
    },
    [
      {
        id: '7'
      },
      {
        id: '8'
      }
    ],
    {
      id: '9'
    },
    {
      id: '10'
    },
    {
      id: '11'
    }
  ]
}
const list = a.view_list.flat()
a.view_list = list
const item = a.view_list.find(item => item.id === '8')
console.log(item)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.