'Javascript filter object by condition on only one of its keys
I have an object with the following structure:
data =
{
columns: array of objects,
rows: [
{
id: 1,
process: "A",
status: "ok"
},
{
id: 2,
process: "A",
status: "ko"
},
{
id: 3,
process: "B",
status: "ok"
},
...
]
}
I then want to create another object that has the same column values, but in the rows field I want to put only the objects that have process === "A".
I've come up with this so far, that I think it's fine:
const filteredData = {
"columns": data.columns,
"rows": selectedProcess !== '' ? data.rows.filter(r => r.process === selectedProcess) : []
}
My question is if there is another way to make this in a more clever way.
Solution 1:[1]
You can check out the solution Below:
const filteredData = {
"columns": data.columns,
"rows": data.rows.filter(item=>item.process==='A')
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mundruku |
