【发布时间】:2019-12-07 10:50:33
【问题描述】:
我有一个包含 JSON 对象的表:
items=[
{"label":"180","quantity":10},
{"label":"50","quantity":35},
{"label":"80","quantity":15},
{"label":"180","quantity":120},
]
label是一个字符串,数量是一个数字,每次用户点击Add按钮时,一个newItem被添加到这个表中+{"label":"80","quantity":11}
我想要的是将这些数据连接成一个字符串,如下所示:str == '180_10,50_35,80_15,180_120'
当用户点击Add str 将是:str == '180_10,50_35,80_15,180_120,80_11'(添加了新项目 80_11)
我的方法不起作用:
addRecup = () => {
const newItem = {
label: this.state.values[this.state.codeValue-1].label,
quantity: this.state.quantite
};
this.setState(state => ({
items: state.items.concat([newItem]), // add new item, it works
}));
let str = '';
let concat = this.state.items.map((item,key) => (
str = str + ',' + this.state.items.label + ',' + this.state.items.quantity // concatenate json doesn't work
));
console.warn('str: ' + str); // got str: undefined,undefined,undefined...
}
【问题讨论】:
标签: javascript json react-native concatenation