【发布时间】:2022-01-13 16:15:33
【问题描述】:
我想要的是加入我从地图函数中获得的数组。
我尝试使用reduce :
const onFinish = (values) => {
values.fields.map((el, idx) => {
console.log({ ...el, index: idx }); // this `console.log` prints me one array after the other
}).reduce((acc, x) => {console.log(acc,x)}); // I tried with `reduce` but it prints me null
也尝试过使用useState:
const onFinish = (values) => {
values.fields.map((el, idx) => {
if (myArray === null){
setMyArray(() => {
return { ...el, index: idx };
});
}
else {
setMyArray(() => {
return {...myArray,...el, index: idx };
});
}
});
console.log(myArray) // at my first call to this const onFinish myArray is printed as [] and at the second it prints with the last object only
};
有人知道如何获取这些对象joined/merged 吗?
【问题讨论】:
-
发布一些示例数据以及您想要的结果
-
看起来
myArray不是一个数组(?) -
@James 这是一个对象,但我想加入对象以显示一个数组
-
@GabrielePetrioli 我编辑了,谢谢你的建议
标签: javascript arrays reactjs json