Reputation: 751
My consult return an json object with array inside array... My console.log
is like this:
item1: false
item2: 999
item3: Array(3)
0: {id: 1589, data: "..."}
1: {id: 1587, data: "..."}
I need get the item3
array and pass like a parameter, so I tried it:
componentDidMount() {
//...
this.myFunction(myJson.item3.map(content => content.id))
}
myFunction = id => {
//something....
}
But, my content
variable is returned like `not defined´.
I tried use different ways to do this, but without successful.
What I'm doing of wrong?
How I can get map()
my array item3
and pass like a parameter to myFunction
?
Upvotes: 1
Views: 81
Reputation: 12900
Assuming your object is structured how you think it is, this should work just fine.
var jsonObj = {
item1: false,
item2: 999,
item3: [
{id: 1589, data: "..."},
{id: 1587, data: "..."}
]
};
const ids = jsonObj.item3.map( o => o.id );
console.log(ids);
If that still doesn't get you your array of ids
please post the exact return of what your json object looks like.
Upvotes: 0
Reputation: 1278
You have not shown enough, I have this code working:
let things = {
thing1: false,
thing2: '999',
thing3: [{ id: 1589, data: "..." },
{ id: 1587, data: "..." }]
}
const myFunction = id => {
console.log(id)
}
myFunction(things.thing3.map(content => content.id))
Upvotes: 0
Reputation: 2828
I think you want something more like this:
componentDidMount() {
//...
myJson.item3.map(content => this.myFunction(content.id))
}
myFunction = id => {
//something....
}
What you are doing in your code is mapping over the array and then doing nothing. Then calling myFunction of undefined since the map never returns anything. What you want to do instead is map the function to each item in the array to get the id. So you would call map and then in the map call myFunction for each item.
Upvotes: 2