BeeNag BeeNag - 3 months ago 13
React JSX Question

Finding values from a nested object

I have an array like so:

[
{
lookups: [],
rows: [{data: {a: 1, b: 2}}, {data: {a: 3, b: 4}}],
title: "Table 1",
columns: [{name: "a"}, {name: "b"}]
},
{
lookups: [],
rows: [{data: {c: 5, d: 6}}, {data: {c: 7, d: 8}}],
title: "Table 2",
columns: [{name: "c"}, {name: "d"}]
}
]


After I select the correct object (which I am using
Array.find()
using the title to do). I need to be able to go through the rows array and try to get each value for a particular string that I have (e.g. If I have a string value of "a" then I would like to get the values 1 and 3 back).

Help would be very much appreciated.

Thanks for your time.

Answer

Use simple mapping. Suppose you have property name stored in variable named prop

data = obj.rows.map(({data}) => data[prop])