根据条件将新的JSON对象插入嵌套的JS数组中
对于我的电子商务应用程序要求之一,我有一个表单的嵌套数组(示例):
const data = [
{
"id": 1,
"group": "upper-wear",
"labels": [
{
"type": "shirts",
"quantity": "20",
},
],
popular: true
},
{
"id": 2,
"group": "bottom-wear",
"lables": [
{
"type": "trousers",
"quantity": "31",
},
],
popular: true
},
]
对于此数组,如果组值等于“上衣”,则需要将新对象插入数组“标签”。
const newDataToInsert = [
{
"type": 'blazers',
"quantity": 19
},
]
这是我到目前为止尝试的,考虑到目前,我只需要插入单个标签(即“上衣”)(将来,可以有多个标签类别“上衣”,“底饰”,“底部磨损”,被插入):
const updatedArray = data.map((datum) => {
if (datum.group === 'upper-wear') {
return {
...datum,
labels: [...datum.labels, ...newDataToInsert]
};
}
});
console.log(updatedArray);
但是,由于结果返回,我似乎缺少一个愚蠢的问题:
[
{
id: 1,
group: 'upper-wear',
labels: [ [Object], [Object] ],
popular: true
},
undefined
]
我知道可能有更好的方法,但这是我现在可以认为的最低解决方案。
任何帮助解决当前或任何更好解决方案的帮助都将受到高度赞赏。
For one of my e-commerce application requirement, I have a nested array of the form (Sample):
const data = [
{
"id": 1,
"group": "upper-wear",
"labels": [
{
"type": "shirts",
"quantity": "20",
},
],
popular: true
},
{
"id": 2,
"group": "bottom-wear",
"lables": [
{
"type": "trousers",
"quantity": "31",
},
],
popular: true
},
]
To this array, I need to insert new objects to the array 'labels' if the group value equals 'upper-wear'.
const newDataToInsert = [
{
"type": 'blazers',
"quantity": 19
},
]
This is what I tried so far, considering that for now I only need to insert to single label (i.e. 'upper-wear') (in future, there can be multiple labels category 'upper-wear', 'bottom-wear', to be inserted into):
const updatedArray = data.map((datum) => {
if (datum.group === 'upper-wear') {
return {
...datum,
labels: [...datum.labels, ...newDataToInsert]
};
}
});
console.log(updatedArray);
But there seems to be a silly issue that I am missing as the result returns like this:
[
{
id: 1,
group: 'upper-wear',
labels: [ [Object], [Object] ],
popular: true
},
undefined
]
I know there may be better approaches available, but this is what I can think of as the minimum solution for now.
any help to resolve the current or any better solution will be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试用这个
解释
这里在映射
数据
时,我们检查条件IF
复制整个对象>b
return { ...b }
lables
return { ...d, labels: d.labels.concat(newDataToInsert) }
,根据 JSON 默认性质,同名的新变量将保存最新值,newDataToInsert
数组合并< code>labels: d.labels.concat(newDataToInsert),它将合并 2 个数组并将它们存储在 JSON 中,名称为labels
Else
Try with this
Explaination
Here while mapping the
data
, we check for the conditionIF
b
return { ...b }
lables
return { ...d, labels: d.labels.concat(newDataToInsert) }
,As per the JSON default nature the new variable with the same name will hold the latest valuenewDataToInsert
arraylabels: d.labels.concat(newDataToInsert)
, It will merge 2 arrays and store them in JSON with the namelabels
Else
else { return d; }
您实际上不需要使用
map
遍历数组。只需在数组中找到一个对象并更改您想要的内容即可。You don't actually need to iterate with
map
over the array. Just find an object in the array and change what you want.您不会返回地图中的所有对象。仅当满足您的条件时您才会返回结果。这会导致您未定义的对象......
You're not returning all objects from your map. you're only returning a result when your criteria is met. This is resulting in your undefined objects...
您可以使用 Array#find 来定位所需的组,然后更改找到的组的标签。有两个选项,具体取决于您要插入的项目数量。使用
Array#push
添加想要的项目;对多个项目使用forEach
:You can use
Array#find
to locate the desired group and then change labels for the group found. There are two options depending on how many items you would like to insert. UseArray#push
to add the desired item; useforEach
for more than one item: