Home > other >  how to find value in nested array and then change the value from boolean to string?
how to find value in nested array and then change the value from boolean to string?

Time:07-21

How change the value in "active" (true to "Yes" /false to "No").

[ { "id":81, "time":"2022-01-01 19:30:00", "subList":[ { "active":false, "success":null } ] }, { "id":89, "time":"2022-01-01 21:00:15", "subList":[ { "active":true, "success":1 } ] } ]

CodePudding user response:

Array.map

Use Array.map or any other looping to loop through the elements of an array.

You can use ? and : Conditional ternary Operator to shorten a simple if else block.

const list = [ { "id":81, "time":"2022-01-01 19:30:00", "subList":[ { "active":false, "success":null } ] }, { "id":89, "time":"2022-01-01 21:00:15", "subList":[ { "active":true, "success":1 } ] } ];

const newList = list.map(item => {
    item.subList.map(subItem => {
        subItem.active = subItem.active ? "Yes" : "No"
        return subItem;
    })
    return item;
})

console.log(newList)

  • Related