The following is a list of users I receive from an API call that gets the list of users from AWS cognito. I want to be able to iterate through it to display the name and email of the user on a web page. I am trying result[0].attributes[3]
to iterate to "given_name", result being the object.
Asked
Active
Viewed 745 times
-1

John Rotenstein
- 241,921
- 22
- 380
- 470

Harsh
- 91
- 1
- 9
-
1`result[0].Attributes.forEach(...)` – Barmar Oct 17 '18 at 21:04
-
1`attributes` ≠ `Attributes` – str Oct 17 '18 at 21:05
-
You iterate through a nested array the same way you iterate through any other array. – Barmar Oct 17 '18 at 21:05
5 Answers
1
You can use filter
to determine if an object property can be found, and then return that object.
result[0].Attributes.filter(obj => obj.name === name);
Here's an example:
let result = [{
Attributes: [{
name: "Mario"
},
{
name: "Luigi"
},
{
name: "Toad"
},
{
name: "Peach"
}
]
}]
function lookfor(name) {
return result[0].Attributes.filter(obj => obj.name === name);
}
console.log(lookfor("Mario"));
console.log(lookfor("Peach"));

zfrisch
- 8,474
- 1
- 22
- 34
-
1How does this relate to the data he shows, where it uses objects like `{Name: "given_name", Value: "Mario"}`? – Barmar Oct 17 '18 at 21:10
-
@Barmar You can filter for any property or properties. The code from OP wasn't provided in code form and I didn't feel like referencing the image back and forth to create an example. It's the same principle. – zfrisch Oct 17 '18 at 21:12
-
1You think someone who needs to ask this question in the first place will be able to extrapolate? – Barmar Oct 17 '18 at 21:14
-
well I mean the someone is getting back a json object from an api, so I would say so. Can you console.log the full result? – Pari Baker Oct 17 '18 at 21:16
-
I guess I'm not understanding the pushback here, haha. Of course I expect OP to extrapolate. If it's over their head they can feel free to comment. – zfrisch Oct 17 '18 at 21:22
0
for(var i in array){
var attr = array[i].Attributes;
for(var l in attr){
// attr[l].Name returns name
// attr[l].Value returns values
}
}

rocky
- 81
- 8
-
[why is using `for-in` with arrays a bad idea](https://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea?lq=1) – Barmar Oct 17 '18 at 21:08
0
You can iterate an array using map
arrributes.map(function(arr) => {
console.log(arr.Name, arr.Value)
})

suresh
- 220
- 2
- 5
0
Object.keys(result).forEach(key => {
let resultObject = result[key].map(array => {
if (array.Name == "given_name") {
console.log(array.value)
} else if (array.Name == "email") {
console.log(array.value);
}
});
});

Pari Baker
- 696
- 4
- 19
-
This won't work. There's no `given_name` or `email` properties, those are the values of the `Name` properties. – Barmar Oct 17 '18 at 21:15
0
const response = [
{ Attributes: [
{Name: 'given_name', Value: 'name 1'},
{Name: 'family_name', Value: 'family 1'},
{Name: 'email', Value: 'email1@gmail.com'}
]
},
{ Attributes: [
{Name: 'given_name', Value: 'name 2'},
{Name: 'family_name', Value: 'family 2'},
{Name: 'email', Value: 'email2@gmail.com'}
]
},
];
const users = response.map((ele) => {
const { Attributes } = ele;
return Attributes.reduce((agg, {Name, Value}) => {
if (Name === 'given_name') {
agg.name = Value;
}
if (Name === 'email') {
agg.email = Value;
}
return agg;
}, {});
});
console.log(users);

Tarek Essam
- 3,602
- 2
- 12
- 21