Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Update object properties in state array - #Solution

By M.K. Verma · 30 April 2022

Update object properties in state array - #Solution

If You are using map() method which return only true or false for that condition. So, You should use filter() method instead of map() method which return filtered data for that condition.

For Example:

const arr = [
    {
        name: 'yes',
        age: 53
    },
    {
        nmae: 'no',
        age: 27
    }
]

const filterByMap = arr.map(elm => elm.age > 18)
console.log(filterByMap) // outputs --> [ true, false ]

const filterByFilter = arr.filter(elm => elm.age > 18)
console.log(filterByFilter) // outputs --> [ { name: 'yes', age: 45 } ]