gpt4 book ai didi

javascript - 如何向数组键添加新值

转载 作者:行者123 更新时间:2023-11-28 17:17:31 24 4
gpt4 key购买 nike

有人可以帮我写一些代码吗?我有一个数组,数组中的状态包含 0-3 之间的不同值,但我想将这些值转换为正确的含义,因此值 1 将完成,2 将不完整,3 将待处理,所以当我在状态上显示数组的详细信息而不是显示数字时,它应该显示完整或不完整等。

这是我的数组:

var prod = [
{
"order_id": "241918",
"product_id": "152737",
"order_qty": "1",
"customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
"out_of_stock": "find_best_match",
"status": "3",
"id": "282",
"sku": "b175a9ea5f4d9b4766e74079c2bec8",
"price": "40.69"
},
{
"order_id": "241918",
"product_id": "155565",
"order_qty": "3",
"customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
"out_of_stock": "find_best_match",
"status": "1",
"id": "283",
"sku": "414a1c04ce7fe72269e116d3dd95d3",
"price": "65.99"
},
{
"order_id": "241918",
"product_id": "148155",
"order_qty": "1",
"customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
"out_of_stock": "find_best_match",
"status": "1",
"id": "285",
"sku": "2477f9462d50d0e7b40631c1a347b2",
"price": "34.86"
},
{
"order_id": "241918",
"product_id": "137556",
"order_qty": "8",
"customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
"out_of_stock": "find_best_match",
"status": "1",
"id": "286",
"sku": "dd8e0b92dfdb2a397d53d9940a588f",
"price": "6.59"
},
{
"order_id": "241918",
"product_id": "153523",
"order_qty": "1",
"customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",
"out_of_stock": "find_best_match",
"status": "3",
"id": "287",
"sku": "3ea54c8856b952820bebf71387d278",
"price": "40.69"
}
]

最佳答案

您可以使用函数Array.prototype.map创建一个具有所需输出的新数组。

基本上,这种方法将创建每个对象的克隆,并使用对象“statuses”(一种映射)根据以下内容将相应的状态分配为字符串( Object.assign )对象的状态。

重要提示:此方法不会改变原始数组或其中的对象。

let arr = [  {    "order_id": "241918",    "product_id": "152737",    "order_qty": "1",    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",    "out_of_stock": "find_best_match",    "status": "3",    "id": "282",    "sku": "b175a9ea5f4d9b4766e74079c2bec8",    "price": "40.69"  },  {    "order_id": "241918",    "product_id": "155565",    "order_qty": "3",    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",    "out_of_stock": "find_best_match",    "status": "1",    "id": "283",    "sku": "414a1c04ce7fe72269e116d3dd95d3",    "price": "65.99"  },  {    "order_id": "241918",    "product_id": "148155",    "order_qty": "1",    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",    "out_of_stock": "find_best_match",    "status": "1",    "id": "285",    "sku": "2477f9462d50d0e7b40631c1a347b2",    "price": "34.86"  },  {    "order_id": "241918",    "product_id": "137556",    "order_qty": "8",    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",    "out_of_stock": "find_best_match",    "status": "1",    "id": "286",    "sku": "dd8e0b92dfdb2a397d53d9940a588f",    "price": "6.59"  },  {    "order_id": "241918",    "product_id": "153523",    "order_qty": "1",    "customer_note": "If they have the berry flavour please take that it is not on website but you got it last week.\nIf not this one is also fine",    "out_of_stock": "find_best_match",    "status": "3",    "id": "287",    "sku": "3ea54c8856b952820bebf71387d278",    "price": "40.69"  }],
statuses = {"1": "complete", "2": "incpmplete", "3": "pending"},
result = arr.map(o => Object.assign({}, o, {status: statuses[o.status]}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 如何向数组键添加新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53103568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com