在JavaScript中,当一个新的orderId被推送时,更新/增加现有的数值。

huangapple go评论69阅读模式
英文:

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 = [{&quot;orderId&quot;:1,&quot;noOfSubmit&quot;:1,&quot;orderNo&quot;:&quot;A&quot;},
{&quot;orderId&quot;:1,&quot;noOfSubmit&quot;:1,&quot;orderNo&quot;:&quot;B&quot;},
{&quot;orderId&quot;:1,&quot;noOfSubmit&quot;:1,&quot;orderNo&quot;:&quot;C&quot;},
{&quot;orderId&quot;:1,&quot;noOfSubmit&quot;:1,&quot;orderNo&quot;:&quot;D&quot;}];
function add_order(order_array, order) {
if (!order_array.some(o =&gt; o.orderId == order.orderId)) {
order_array.forEach(o =&gt; o.noOfSubmit++);
}
order_array.push(order);
}
add_order(orders, {&quot;orderId&quot;:2,&quot;noOfSubmit&quot;:1, &quot;orderNo&quot;:&quot;E&quot;});
add_order(orders, {&quot;orderId&quot;:2,&quot;noOfSubmit&quot;:1, &quot;orderNo&quot;:&quot;F&quot;});
add_order(orders, {&quot;orderId&quot;:3,&quot;noOfSubmit&quot;:1, &quot;orderNo&quot;:&quot;G&quot;});
add_order(orders, {&quot;orderId&quot;:3,&quot;noOfSubmit&quot;:1, &quot;orderNo&quot;:&quot;H&quot;});
console.log(orders);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月9日 06:34:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定