[英]How to append an iterator index number to each item within an array with Javascript?
[英]How to append an object to each item in an array
我正在嘗試將一個對象附加到一個數組中,但是cart[i]['items'][j][order]
沒有正確設置。
當記錄cart[i]['order']
它返回一個Object
。
購物車的形式為:
[{
"order": {
"amtSubTotal": 3812.8900000000003,
"noPo": "Default P.O."
},
"items": [{
"adjustments": { "isCustom": "N" },
"lineNote": { "noteLine": "" }
},{
"adjustments": { "isCustom": "N" },
"lineNote": { "noteLine": "" }
}]
}]
預期結果
[{
"order": { "amtSubTotal": 3812.8900000000003,"noPo": "Default P.O." },
"adjustments": { "isCustom": "N" },
"lineNote": { "noteLine": "" }
}, {
"order": { "amtSubTotal": 3812.8900000000003,"noPo": "Default P.O." },
"adjustments": { "isCustom": "N" },
"lineNote": { "noteLine": "" }
}]
代碼
let cart = JSON.parse(localStorage.getItem('cart-data'));
let i = 0, j = 0, l = cart.length;
let nData = [];
for (i = 0; i < l; i++) {
let m = cart[i]['items'].length;
for (j = 0; j < m; j++) { // Spreading data {...data[i]} created a shallow copy bug
cart[i]['items'][j].push({"order": cart[i]['order']}); // Try using JSON methods instead
nData.push(cart[i]['items'][j]);
}
}
替代嘗試
for (i = 0; i < l; i++) {
let m = cart[i]['items'].length;
for (j = 0; j < m; j++) {
// cart[i]['items'][j]['order'] = JSON.stringify(JSON.parse(cart[i]['order'])); // Try using JSON methods instead
nData.push(cart[i]['items'][j]);
}
}
console.log(nData); // order data not appended to cart[i]['items'][j]
如何將此對象添加到每個cart[i]['items'][j]
元素?
您的代碼在推車時生成錯誤cart[i]['items'][j].push(...)
刪除[j]
嘗試這個:
let carts = [{ order: { amtSubTotal: 3812.8900000000003, noPo: "Default PO" }, items: [{ adjustments: { "isCustom": "N" }, lineNote: { "noteLine": "" } },{ adjustments: { "isCustom": "N" }, lineNote: { "noteLine": "" } }] }]; nData = []; carts.forEach((cart, idx1) => { cart['items'].forEach((item, idx2) => { item.order = cart.order; nData.push(item) }) }); console.log(nData);
使用 map() 在每次迭代時修改您的對象,並添加 items 對象的屬性迭代通過鍵值for(let [key, value] of Object.entries(itemObj)
嘗試運行此代碼段。 我在購物車中添加了第二個項目以進行測試
let cart=[{"order": {"amtSubTotal": 3812.8900000000003,"noPo": "Default PO"},"items": [{"adjustments": { "isCustom": "N" }, "lineNote": { "noteLine": "" }},{"adjustments": { "isCustom": "N" }, "lineNote": { "noteLine": "" }}]},{"order": {"amtSubTotal": 3112.8900000000003,"noPo": "Default RO"},"items": [{"adjustments": { "isCustom": "Y" }, "lineNote": { "noteLine": "1" }},{"adjustments": { "isCustom": "N" }, "lineNote": { "noteLine": "5" }}]}] let result=cart.map( orderObj =>{ let obj={order:orderObj.order}; orderObj.items.forEach( itemObj =>{ for(let [key, value] of Object.entries(itemObj)) obj[key]=value; }); return obj }); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您可以嘗試如下解構和映射。 (在箭頭函數參數中,解構order
和items
,使用map
覆蓋 items 並添加 order 的內容。)
const update = ([{ order, items }]) => items.map(itm => ({ order: {...order}, ...itm })); const data = [ { order: { amtSubTotal: 3812.8900000000003, noPo: "Default PO" }, items: [ { adjustments: { isCustom: "N" }, lineNote: { noteLine: "" } }, { adjustments: { isCustom: "N" }, lineNote: { noteLine: "" } } ] } ]; console.log(update(data));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.