admin管理员组文章数量:1406950
I am fetching an array of objects in the state, and I am trying to add a property to each of these objects.
The problem is I can see my property being added to each of the objects but when I mapping through all these objects and trying to console.log
it I'm getting undefined
Adding a property
addFavourites() {
let data = this.state.data.map((e) => {
e.favourite = false;
return e;
});
return data;
}
state = {
data: []
}
addFavourites's called here:
getItem = () => {
this.service
.mergeData()
.then((body) => {
this.setState({
data: body
},
() => {
this.addFavourites();
},
() => {
this.dataToFilter();
});
});
}
ponentDidMount(){
this.getItem();
}
In render function I see all my objects including favourite
console.log(data.map((e) => e));
But this gives an array of undefined
console.log(data.map((e) => e.favourite));
How can I solve this?
I am fetching an array of objects in the state, and I am trying to add a property to each of these objects.
The problem is I can see my property being added to each of the objects but when I mapping through all these objects and trying to console.log
it I'm getting undefined
Adding a property
addFavourites() {
let data = this.state.data.map((e) => {
e.favourite = false;
return e;
});
return data;
}
state = {
data: []
}
addFavourites's called here:
getItem = () => {
this.service
.mergeData()
.then((body) => {
this.setState({
data: body
},
() => {
this.addFavourites();
},
() => {
this.dataToFilter();
});
});
}
ponentDidMount(){
this.getItem();
}
In render function I see all my objects including favourite
console.log(data.map((e) => e));
But this gives an array of undefined
console.log(data.map((e) => e.favourite));
How can I solve this?
Share edited Aug 5, 2019 at 16:14 Gershon Papi 5,1263 gold badges27 silver badges51 bronze badges asked Aug 5, 2019 at 15:58 StanStan 1301 gold badge2 silver badges9 bronze badges 3-
Would you provide more code how
addFavorite
is called, when the state is saved, and where you are logging? – dance2die Commented Aug 5, 2019 at 16:01 - 1 I edit my code, is it ok? – Stan Commented Aug 5, 2019 at 16:04
-
You call
this.addFavourites();
but returned data fromaddFavourites()
never get stored in any variable ? – Flo Commented Aug 5, 2019 at 16:06
1 Answer
Reset to default 5First, wele! You should have probably made it more clear that this question is about React.
Now to the answer, every state modification would be made using setState
, so your addFavourites
function should be like this:
addFavourites() {
let data = this.state.data.map((e) => {
e.favourite = false;
return e;
});
this.setState({ data: data });
}
本文标签: javascriptReactAdding a property to an object inside of array in component39s stateStack Overflow
版权声明:本文标题:javascript - React - Adding a property to an object inside of array in component's state - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744950376a2634046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论