我在使用 FOR 将数据输入对象时遇到问题
2022-05-13
63
我有一个对象,我想使用 FOR 循环遍历它 因为我想根据自己的需要更改对象
这是我的原始对象
我从 console.log() 复制了它
(7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
[
{Order: 14, Address: 100101, SKU: 'CJNSXZHL', CJ Quantity: '1', Store: 'go', …}
{SKU: 'CJNSX', CJ Quantity: '1', CJ Product Name: 'bbb', __rowNum__: 2}
{Order: 15, Address: 100102, SKU: 'CJNSXZN', CJ Quantity: '1', Store: 'go', …}
{SKU: 'CJNS', CJ Quantity: '1', CJ Product Name: 'ddd', __rowNum__: 4}
{Order: 16, Address: 100103, SKU: 'CJNSX', CJ Quantity: '1', Store: 'go', …}
{SKU: 'CJNS', CJ Quantity: '1', CJ Product Name: 'fff', __rowNum__: 6}
{Order: 17, Address: 100104, SKU: 'CJNSTX', CJ Quantity: 2, Store: 'go', …}
]
现在我想做的是检查我是否在 ORDER 中未定义 您可以看到,在第一行中,它设置在第二个 no 中,如果没有,则获取第二行中 SKU 中的值并将其插入数组中的先前位置 我将附上我编写的代码
const arr = [];
for ( let i=0 ; i <= items.length-1;i++) {
try {
if (items[i].Order !== undefined){
console.log(" have ",i)
arr.push({
"CJ Order Number":items[i].Order,
"Address":items[i].Address,
"SKU":[items[i].SKU],
});
}
else if (items[i].Order === undefined){
console.log("dont have ",i)
arr[i-1].SKU.push(items[i].SKU)
}
}catch (err){
console.log("err",i)
}
}
console.log(arr)
只在第一行中发生的情况是它成功了 但在第三次迭代中,应该进入第二行,它没有成功,我收到错误
“TypeError:无法读取属性undefined (reading 'SKU')"
而奇怪的是,他在第一行就成功了 这里是来自console.log()的代码
我把第一行打开了,这样你就能看到他输入的SKU值在下一行
(4) [{…}, {…}, {…}, {…}]
0:
Address: 100101
CJ Order Number: 14
SKU: (2) ['CJNSXZHL', 'CJNSX']
[[Prototype]]: Object
1: {CJ Order Number: 15, Address: 100102, SKU: Array(1)}
2: {CJ Order Number: 16, Address: 100103, SKU: Array(1)}
3: {CJ Order Number: 17, Address: 100104, SKU: Array(1)}
length: 4
我也一步一步跟着 任意集成
have 0
1 'dont have'
have 2
3 'dont have'
err 3
have 4
5 'dont have'
err 5
have 6
3个回答
let items = [{
Order: 14,
Address: 100101,
SKU: 'CJNSXZHL',
'CJ Quantity': '1',
Store: 'go',
},
{
SKU: 'CJNSX',
'CJ Quantity': '1',
'CJ Product Name': 'bbb',
__rowNum__: 2
},
{
Order: 15,
Address: 100102,
SKU: 'CJNSXZN',
'CJ Quantity': '1',
Store: 'go',
},
{
SKU: 'CJNS',
'CJ Quantity': '1',
'CJ Product Name': 'ddd',
__rowNum__: 4
},
{
Order: 16,
Address: 100103,
SKU: 'CJNSX',
'CJ Quantity': '1',
Store: 'go'
},
{
SKU: 'CJNS',
'CJ Quantity': '1',
'CJ Product Name': 'fff',
__rowNum__: 6
},
{
Order: 17,
Address: 100104,
SKU: 'CJNSTX',
'CJ Quantity': 2,
Store: 'go'
},
];
const arr = [];
for (let i = 0; i <= items.length - 1; i++) {
if (items[i].Order) {
arr.push({
'CJ Order Number': items[i].Order,
Address: items[i].Address,
SKU: [items[i].SKU],
});
} else {
arr[arr.length - 1].SKU.push(items[i].SKU); // the last element
}
}
console.log(arr);
Try this.
Nizamuddin Shaikh
2022-05-15
希望这是您要做的,您可以尝试这样做,但可能会有更好的解决方案。
const arr = [];
const items = [
{
Order: 14,
Address: 100101,
SKU: "CJNSXZHL",
CJ_Quantity: "1",
Store: "go",
},
{ SKU: "CJNSX", CJ_Quantity: "1", CJ_Product_Name: "bbb", __rowNum__: 2 },
{
SKU: "CJNSXZN",
Order: 15,
Address: 100102,
CJ_Quantity: "1",
Store: "go",
},
{ SKU: "CJNS", CJ_Quantity: "1", CJ_Product_Name: "ddd", __rowNum__: 4 },
{ Order: 16, Address: 100103, SKU: "CJNSX", CJ_Quantity: "1", Store: "go" },
{ SKU: "CJNS", CJ_Quantity: "1", CJ_Product_Name: "fff", __rowNum__: 6 },
{ Order: 17, Address: 100104, SKU: "CJNSTX", CJ_Quantity: 2, Store: "go" },
];
for (let i = 0, j = 1; i <= items.length - 1; i++) {
try {
if (items[i].Order) {
arr.push({
"CJ Order Number": items[i].Order,
Address: items[i].Address,
SKU: [items[i].SKU],
});
} else {
arr[j-1].SKU.push(items[i].SKU);
j++;
}
} catch (err) {
console.log("err", err);
}
}
console.log(arr);
Abderrahmen Mhemed
2022-05-13
您可以使用
reduce
const data = [{
Order: 14,
Address: 100101,
SKU: 'CJNSXZHL',
'CJ Quantity': '1',
Store: 'go'
},
{
SKU: 'CJNSX',
'CJ Quantity': '1',
'CJ Product Name': 'bbb',
__rowNum__: 2
},
{
Order: 15,
Address: 100102,
SKU: 'CJNSXZN',
'CJ Quantity': '1',
Store: 'go'
},
{
SKU: 'CJNS',
'CJ Quantity': '1',
'CJ Product Name': 'ddd',
__rowNum__: 4
},
{
Order: 16,
Address: 100103,
SKU: 'CJNSX',
'CJ Quantity': '1',
Store: 'go'
},
{
SKU: 'CJNS',
'CJ Quantity': '1',
'CJ Product Name': 'fff',
__rowNum__: 6
},
{
Order: 17,
Address: 100104,
SKU: 'CJNSTX',
'CJ Quantity': 2,
Store: 'go'
}
]
const result = data.reduce((res, item) => {
if (item.Order) {
return [...res, {...item, SKU:[]}]
}
const lastOrder = res[res.length - 1]
res[res.length - 1].SKU = [...(lastOrder.SKU || []), item]
return res
}, [])
console.log(result)
轻松完成此操作
R4ncid
2022-05-13