0

I have following function:

let newArray = apiArr
                .filter(function (data) {
                    if (data.parentId == detailsOfSelectedValue.id) {
                        return data
                    }
                })
                .map(function (item) {
                    return item.name
                })

Here, Filter condition is right, after filter in map I want name and type both in return.

How can I do that?

Trupti
  • 843
  • 2
  • 11
  • 28
  • 2
    `.map(({name, type}) => ({name, type}))` Also, `filter()` should return Boolean, not a value `.filter((data) => data.parentId === detailsOfSelectedValue.id)` – Roko C. Buljan Aug 31 '23 at 07:45
  • 2
    FYI, the `filter` callback is supposed to return **`true` or `false`**, not the data or nothing. That just happens to work in this case. – deceze Aug 31 '23 at 07:45
  • 1
    Thank you @RokoC.Buljan. It solved my issue. – Trupti Aug 31 '23 at 07:59
  • @3limin4t0r, this is different than what you have given as reference. I checked that, it was not what I required. – Trupti Aug 31 '23 at 08:00
  • @Trupti you're very welcome. Read: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#object_destructuring the part talking about: *"Unpacking properties from objects passed as a function parameter"* – Roko C. Buljan Aug 31 '23 at 08:10
  • @Trupti your question never specified you want an object with properties. Even then several https://stackoverflow.com/a/2917197, https://stackoverflow.com/a/49051298, https://stackoverflow.com/a/39803959, https://stackoverflow.com/a/25720277, https://stackoverflow.com/a/57512157, https://stackoverflow.com/a/37644852, https://stackoverflow.com/a/56747883, https://stackoverflow.com/a/46979878, https://stackoverflow.com/a/51416677, https://stackoverflow.com/a/55517905, https://stackoverflow.com/a/56785420, https://stackoverflow.com/a/51360131 answers show that. – VLAZ Aug 31 '23 at 08:12

0 Answers0