我正在尝试从对象中获取特定字段的数据。
- 从对象中获取特定字段的数据
- 如果可能的话,一次性使用获得的数据设置状态
这里是我想要编辑(使用setState)的taggedItems
。
taggedItems = {
0: { id: 0, width: 40, height: 40, image: null, base64: null },
1: { id: 1, width: 200, height: 200, image: null, base64: null },
2: { id: 2, width: 80, height: 80, image: null, base64: null }
}
我正在从图像中裁剪 taggedItems
。我得到了image
和base64
数据。
for (var key in taggedItems) {
..
// Get successURI, base64 value
// Store it to array or object (Can store them in array, object or any type.
// Because I don't prefer to setState in the for-loop.
// exampleArray = [
// { id: 0, image: 'file:path1', base64: 'ksdnflksdf' },
// { id: 1, image: 'file:path2', base64: 'fldmlkflkj' },
// { id:2, image: 'file:path3', base64: 'glkmdlfg' },
// ]
// HERE IS THE PROBLEM: I want to setState all of items at once(if it's possible)
setState({taggedItems: ???});
在上面调用
setState
之后,this.state.taggedItems
的预期输出将为:taggedItems = {
0: { id: 0, width: 40, height: 40, image: 'file:path1', base64: 'ksdnflksdf' },
1: { id: 1, width: 200, height: 200, image: 'file:path2', base64: 'fldmlkflkj' },
2: { id: 2, width: 80, height: 80, image: 'file:path3', base64: 'glkmdlfg' }
}
无论如何我都会将对象转换为数组,以便将数据发送到JSON中,所以请随意使用任何类型的变量(数组或对象)。