将新属性添加到现有的对象数组中
我在我的React应用程序中使用formik,并具有以下初始值,这只是其中的一部分:
"myInfo": [
{
"myId": 0,
"myName": "Name A",
"defaultVal": 2
},
{
"myId": 1,
"myName": "Name B",
"defaultVal": 4
}
]
我试图做的是尝试在此数组中添加新属性,但运气不多。
我的代码如下:
const getAdditionalData = async (id, idx) => {
try {
const response = await fetch(`http://localhost:3000/more-data/${id}`);
const myData = await response.json();
myInfo[idx].vals = myData;
} catch (err) {
console.error(err.message);
}
};
useEffect(() => {
myInfo.map((info, index) => {
getAdditionalData(info.myId, index)
});
}, []);
fyi,返回mydata
中的值是另一个看起来像这样的对象的数组:
myid
= 0,它返回:
[{id: 1, name: 'AA'}
{id: 2, name: 'BB'}]
wher code> myid < /code> = 1它返回:
[{id: 3, name: 'CC'}
{id: 4, name: 'DD'}
{id: 5, name: 'EE'}]
我试图做的是在数组循环中,我想在myInfo
array中添加一个新属性。我尝试了以下内容,但无效:
myInfo [idx] .vals = mydata;
我追求的最终结果如下:
"myInfo": [
{
"myId": 0,
"myName": "Name A",
"defaultVal": 2,
"vals": [
{id: 1, name: 'AA'},
{id: 2, name: 'BB'}
]
},
{
"myId": 1,
"myName": "Name B",
"defaultVal": 4,
"vals": [
{id: 3, name: 'CC'},
{id: 4, name: 'DD'},
{id: 5, name: 'EE'}
]
}
]
您会看到vals的新属性
已添加。
任何帮助都将不确定我缺少什么。
I am using Formik within my React app and have the following initial values, which is just a portion of it:
"myInfo": [
{
"myId": 0,
"myName": "Name A",
"defaultVal": 2
},
{
"myId": 1,
"myName": "Name B",
"defaultVal": 4
}
]
What I am attempting to do is to try and add a new property to this array of objects but not having much luck.
My code is as follows:
const getAdditionalData = async (id, idx) => {
try {
const response = await fetch(`http://localhost:3000/more-data/${id}`);
const myData = await response.json();
myInfo[idx].vals = myData;
} catch (err) {
console.error(err.message);
}
};
useEffect(() => {
myInfo.map((info, index) => {
getAdditionalData(info.myId, index)
});
}, []);
FYI, the values returned in myData
is another array of objects that looks like this:
when myId
= 0 it returns:
[{id: 1, name: 'AA'}
{id: 2, name: 'BB'}]
when myId
= 1 it returns:
[{id: 3, name: 'CC'}
{id: 4, name: 'DD'}
{id: 5, name: 'EE'}]
What I am attempting to do is that while within the array loop, I want to add a new property within myInfo
array. I tried the following, which didn't work:
myInfo[idx].vals = myData;
The end result that I am after is the following:
"myInfo": [
{
"myId": 0,
"myName": "Name A",
"defaultVal": 2,
"vals": [
{id: 1, name: 'AA'},
{id: 2, name: 'BB'}
]
},
{
"myId": 1,
"myName": "Name B",
"defaultVal": 4,
"vals": [
{id: 3, name: 'CC'},
{id: 4, name: 'DD'},
{id: 5, name: 'EE'}
]
}
]
You will see that a new property of vals
has been added.
Any help would be much appreciated as not sure what I am missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
什么是
myInfo
?状态,班级或参考变量?还要考虑到MAP
返回一个全新的数组,因此不会突变。您可以尝试此操作,应该在mynewinfo中查看更新的阵列:
What is
myInfo
? A state, class or ref variable? Also take into consideration thatmap
returns a brand new array so it doesn't mutate.You can try this and you should see the updated array in myNewInfo:
以下是本机JavaScript中我从我这里奏效的内容:
请注意,您需要同步
myInfo
和valobj
的索引。Here's what worked from me in native javascript:
Note that you would need to sync the indices of
myInfo
andvalObj
.看来您正在尝试使用像对象这样的JSON字符串。他们必须是
请参阅以下示例。
It looks like you are attempting to use JSON strings like objects. They have to be parsed first.
See the following example.