英文:
Updated/increment the existing value when a new orderId is pushed in Javascript
问题
这是您要的翻译结果:
// 输入数组
[
{"orderId": 1, "noOfSubmit": 1, "orderNo": "A"},
{"orderId": 1, "noOfSubmit": 1, "orderNo": "B"},
{"orderId": 1, "noOfSubmit": 1, "orderNo": "C"},
{"orderId": 1, "noOfSubmit": 1, "orderNo": "D"}
]
// 将以下对象推送到上述数组
{"orderId": 2, "noOfSubmit": 1, "orderNo": "E"}
// 期望输出
[
{"orderId": 1, "noOfSubmit": 2, "orderNo": "A"},
{"orderId": 1, "noOfSubmit": 2, "orderNo": "B"},
{"orderId": 1, "noOfSubmit": 2, "orderNo": "C"},
{"orderId": 1, "noOfSubmit": 2, "orderNo": "D"},
{"orderId": 2, "noOfSubmit": 1, "orderNo": "E"}
]
// 将以下对象推送到上述数组
{"orderId": 2, "noOfSubmit": 1, "orderNo": "F"}
// 期望输出
[
{"orderId": 1, "noOfSubmit": 2, "orderNo": "A"},
{"orderId": 1, "noOfSubmit": 2, "orderNo": "B"},
{"orderId": 1, "noOfSubmit": 2, "orderNo": "C"},
{"orderId": 1, "noOfSubmit": 2, "orderNo": "D"},
{"orderId": 2, "noOfSubmit": 1, "orderNo": "E"},
{"orderId": 2, "noOfSubmit": 1, "orderNo": "F"}
]
// 推送以下对象到上述数组
{"orderId": 3, "noOfSubmit": 1, "orderNo": "G"}
// 期望输出
[
{"orderId": 1, "noOfSubmit": 3, "orderNo": "A"},
{"orderId": 1, "noOfSubmit": 3, "orderNo": "B"},
{"orderId": 1, "noOfSubmit": 3, "orderNo": "C"},
{"orderId": 1, "noOfSubmit": 3, "orderNo": "D"},
{"orderId": 2, "noOfSubmit": 2, "orderNo": "E"},
{"orderId": 2, "noOfSubmit": 2, "orderNo": "F"},
{"orderId": 3, "noOfSubmit": 1, "orderNo": "G"}
]
// 推送以下对象到上述数组
{"orderId": 3, "noOfSubmit": 1, "orderNo": "H"}
// 期望输出
[
{"orderId": 1, "noOfSubmit": 3, "orderNo": "A"},
{"orderId": 1, "noOfSubmit": 3, "orderNo": "B"},
{"orderId": 1, "noOfSubmit": 3, "orderNo": "C"},
{"orderId": 1, "noOfSubmit": 3, "orderNo": "D"},
{"orderId": 2, "noOfSubmit": 2, "orderNo": "E"},
{"orderId": 2, "noOfSubmit": 2, "orderNo": "F"},
{"orderId": 3, "noOfSubmit": 1, "orderNo": "G"},
{"orderId": 3, "noOfSubmit": 1, "orderNo": "H"}
]
// 推送以下对象到上述数组
{"orderId": 2, "noOfSubmit": 1, "orderNo": "I"}
// 最终输出
[
{"orderId": 1, "noOfSubmit": 3, "orderNo": "A"},
{"orderId": 1, "noOfSubmit": 3, "orderNo": "B"},
{"orderId": 1, "noOfSubmit": 3, "orderNo": "C"},
{"orderId": 1, "noOfSubmit": 3, "orderNo": "D"},
{"orderId": 2, "noOfSubmit": 2, "orderNo": "E"},
{"orderId": 2, "noOfSubmit": 2, "orderNo": "F"},
{"orderId": 3, "noOfSubmit": 1, "orderNo": "G"},
{"orderId": 3, "noOfSubmit": 1, "orderNo": "H"},
{"orderId": 2, "noOfSubmit": 1, "orderNo": "I"}
]
英文:
here is the array with the objects, and when i push a new object with order 2, i would like to increment the noOfSubmit whenever a new orderId is pushed , is there a way to achieve the following using javascript?
Input array
[{"orderId":1,"noOfSubmit":1,"orderNo":"A"},
{"orderId":1,"noOfSubmit":1,"orderNo":"B"},
{"orderId":1,"noOfSubmit":1,"orderNo":"C"},
{"orderId":1,"noOfSubmit":1,"orderNo":"D"}]
{"orderId":2,"noOfSubmit":1,"orderNo":"E"}
- Push this to the array above
Expected output:
[{"orderId":1,"noOfSubmit":2,"orderNo":"A"},
{"orderId":1,"noOfSubmit":2,"orderNo":"B"},
{"orderId":1,"noOfSubmit":2,"orderNo":"C"},
{"orderId":1,"noOfSubmit":2,"orderNo":"D"},
{"orderId":2,"noOfSubmit":1,"orderNo":"E"}]
{"orderId":2,"noOfSubmit":1,"orderNo":"F"}
- Push this next to the same array above
Expected output:
[{"orderId":1,"noOfSubmit":2,"orderNo":"A"},
{"orderId":1,"noOfSubmit":2,"orderNo":"B"},
{"orderId":1,"noOfSubmit":2,"orderNo":"C"},
{"orderId":1,"noOfSubmit":2,"orderNo":"D"},
{"orderId":2,"noOfSubmit":1,"orderNo":"E"},
{"orderId":2,"noOfSubmit":1,"orderNo":"F"}]
{"orderId":3,"noOfSubmit":1, "orderNo":"G"}
--> now push this object
Expected output:
[{"orderId":1,"noOfSubmit":3,"orderNo":"A"},
{"orderId":1,"noOfSubmit":3,"orderNo":"B"},
{"orderId":1,"noOfSubmit":3,"orderNo":"C"},
{"orderId":1,"noOfSubmit":3,"orderNo":"D"},
{"orderId":2,"noOfSubmit":2,"orderNo":"E"},
{"orderId":2,"noOfSubmit":2,"orderNo":"F"},
{"orderId":3,"noOfSubmit":1,"orderNo":"G"} ]
{"orderId":3,"noOfSubmit":1, "orderNo":"H"}
--> now push this object
Expected output:
[{"orderId":1,"noOfSubmit":3,"orderNo":"A"},
{"orderId":1,"noOfSubmit":3,"orderNo":"B"},
{"orderId":1,"noOfSubmit":3,"orderNo":"C"},
{"orderId":1,"noOfSubmit":3,"orderNo":"D"},
{"orderId":2,"noOfSubmit":2,"orderNo":"E"},
{"orderId":2,"noOfSubmit":2,"orderNo":"F"},
{"orderId":3,"noOfSubmit":1, "orderNo":"G"},
{"orderId":3,"noOfSubmit":1, "orderNo":"H"} ]
**Edit: When a new order Id is pushed it should increment noOfSubmit like you advised, but at the same time it should also verify all the array records if orderId matches the first character after split by hyphen, increment noOfSubmit only for the records that matches. eg:"orderId":R1-1 (after split by -, in this case R1).
when R2 records are pushed, it should not auto increment noOfSubmit of the array records where orderId starts with R1.
likewise when R3- OrderId records are pushed, it should not increment noOfSubmit for all the records where OrderId includes to R1 and R2 after applying split by hyphen.
Input array
[{"orderId":R1-1,"noOfSubmit":1,"orderNo":"A"},
{"orderId":R1-1,"noOfSubmit":1,"orderNo":"B"},
{"orderId":R1-1,"noOfSubmit":1,"orderNo":"C"},
{"orderId":R1-1,"noOfSubmit":1,"orderNo":"D"}]
{"orderId":R1-2,"noOfSubmit":1,"orderNo":"E"}
- Push this to the array above
Expected output:
[{"orderId":R1-1,"noOfSubmit":2,"orderNo":"A"},
{"orderId":R1-1,"noOfSubmit":2,"orderNo":"B"},
{"orderId":R1-1,"noOfSubmit":2,"orderNo":"C"},
{"orderId":R1-1,"noOfSubmit":2,"orderNo":"D"},
{"orderId":R1-2,"noOfSubmit":1,"orderNo":"E"}]
{"orderId":R1-2,"noOfSubmit":1,"orderNo":"F"}
- Push this to the array above
Expected output:
[{"orderId":R1-1,"noOfSubmit":2,"orderNo":"A"},
{"orderId":R1-1,"noOfSubmit":2,"orderNo":"B"},
{"orderId":R1-1,"noOfSubmit":2,"orderNo":"C"},
{"orderId":R1-1,"noOfSubmit":2,"orderNo":"D"},
{"orderId":R1-2,"noOfSubmit":1,"orderNo":"E"},
{"orderId":R1-2,"noOfSubmit":1,"orderNo":"F"},]
{"orderId":R1,"noOfSubmit":1,"orderNo":"G"}
- Push this to the array above
Expected output:
[{"orderId":R1-1,"noOfSubmit":3,"orderNo":"A"},
{"orderId":R1-1,"noOfSubmit":3,"orderNo":"B"},
{"orderId":R1-1,"noOfSubmit":3,"orderNo":"C"},
{"orderId":R1-1,"noOfSubmit":3,"orderNo":"D"},
{"orderId":R1-2,"noOfSubmit":2,"orderNo":"E"},
{"orderId":R1-2,"noOfSubmit":2,"orderNo":"F"},
{"orderId":R1,"noOfSubmit":1,"orderNo":"G"}]
{"orderId":R2,"noOfSubmit":1,"orderNo":"H"}
- Push this to the array above
[{"orderId":R1-1,"noOfSubmit":3,"orderNo":"A"},
{"orderId":R1-1,"noOfSubmit":3,"orderNo":"B"},
{"orderId":R1-1,"noOfSubmit":3,"orderNo":"C"},
{"orderId":R1-1,"noOfSubmit":3,"orderNo":"D"},
{"orderId":R1-2,"noOfSubmit":2,"orderNo":"E"},
{"orderId":R1-2,"noOfSubmit":2,"orderNo":"F"},
{"orderId":R1,"noOfSubmit":1,"orderNo":"G"}
{"orderId":R2,"noOfSubmit":1,"orderNo":"H"}]
{"orderId":R2,"noOfSubmit":1,"orderNo":"I"}
- Push this to the array above
[{"orderId":R1-1,"noOfSubmit":3,"orderNo":"A"},
{"orderId":R1-1,"noOfSubmit":3,"orderNo":"B"},
{"orderId":R1-1,"noOfSubmit":3,"orderNo":"C"},
{"orderId":R1-1,"noOfSubmit":3,"orderNo":"D"},
{"orderId":R1-2,"noOfSubmit":2,"orderNo":"E"},
{"orderId":R1-2,"noOfSubmit":2,"orderNo":"F"},
{"orderId":R2,"noOfSubmit":1,"orderNo":"G"},
{"orderId":R2,"noOfSubmit":1,"orderNo":"I"}]
{"orderId":R3,"noOfSubmit":1,"orderNo":"I"}
- Push this to the array above
[{"orderId":R1-1,"noOfSubmit":3,"orderNo":"A"},
{"orderId":R1-1,"noOfSubmit":3,"orderNo":"B"},
{"orderId":R1-1,"noOfSubmit":3,"orderNo":"C"},
{"orderId":R1-1,"noOfSubmit":3,"orderNo":"D"},
{"orderId":R1-2,"noOfSubmit":2,"orderNo":"E"},
{"orderId":R1-2,"noOfSubmit":2,"orderNo":"F"},
{"orderId":R2,"noOfSubmit":1,"orderNo":"G"},
{"orderId":R2,"noOfSubmit":1,"orderNo":"I"},
{"orderId":R3,"noOfSubmit":1,"orderNo":"I"}]
答案1
得分: 1
在推送新对象之前,搜索现有数组以确定订单ID是否是新的。如果是新的,遍历数组并递增所有noOfSubmit
,然后推送新项。
const orders = [
{"orderId": 1, "noOfSubmit": 1, "orderNo": "A"},
{"orderId": 1, "noOfSubmit": 1, "orderNo": "B"},
{"orderId": 1, "noOfSubmit": 1, "orderNo": "C"},
{"orderId": 1, "noOfSubmit": 1, "orderNo": "D"}
];
function add_order(order_array, order) {
if (!order_array.some(o => o.orderId == order.orderId)) {
order_array.forEach(o => o.noOfSubmit++);
}
order_array.push(order);
}
add_order(orders, {"orderId": 2, "noOfSubmit": 1, "orderNo": "E"});
add_order(orders, {"orderId": 2, "noOfSubmit": 1, "orderNo": "F"});
add_order(orders, {"orderId": 3, "noOfSubmit": 1, "orderNo": "G"});
add_order(orders, {"orderId": 3, "noOfSubmit": 1, "orderNo": "H"});
console.log(orders);
请注意,这是您提供的JavaScript代码的翻译。
英文:
Before pushing the new object, search the existing array to determine if the order ID is new. If it is, go through the array and increment all the noOfSubmit
. Then push the new item.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const orders = [{"orderId":1,"noOfSubmit":1,"orderNo":"A"},
{"orderId":1,"noOfSubmit":1,"orderNo":"B"},
{"orderId":1,"noOfSubmit":1,"orderNo":"C"},
{"orderId":1,"noOfSubmit":1,"orderNo":"D"}];
function add_order(order_array, order) {
if (!order_array.some(o => o.orderId == order.orderId)) {
order_array.forEach(o => o.noOfSubmit++);
}
order_array.push(order);
}
add_order(orders, {"orderId":2,"noOfSubmit":1, "orderNo":"E"});
add_order(orders, {"orderId":2,"noOfSubmit":1, "orderNo":"F"});
add_order(orders, {"orderId":3,"noOfSubmit":1, "orderNo":"G"});
add_order(orders, {"orderId":3,"noOfSubmit":1, "orderNo":"H"});
console.log(orders);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论