如何向数组键添加新值

时间:2018-11-01 14:42:36

标签: javascript arrays

有人可以帮我提供一些代码吗?我有一个数组,并且在数组中有一个状态,该状态持有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"
}
]

4 个答案:

答案 0 :(得分:2)

您可以使用函数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; }

答案 1 :(得分:1)

您可以创建一个“ statusMap”对象来定义要映射的值,如下所示,然后在数组上循环(forEach)

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"
  }
]

var statusMap = { 1: 'complete', 2: 'incomplete', 3: 'pending' }

prod.forEach(d => d['status'] = statusMap[d['status']])

console.log(prod)

答案 2 :(得分:0)

您可以使用forEach()遍历数组,并根据需要修改状态

prod.forEach((eachOb) => {
    if (eachOb.status == 1) {
        eachOb.status = 'complete'
    }
    else if (eachOb.status == 1) {
        eachOb.status = 'incomplete'
    }
    else if (eachOb.status == 1) {
        eachOb.status = 'pending'
    }
    else {
          // something for 0
    }

})

答案 3 :(得分:0)

  function fromNumberToString(statusNumber){
    switch(statusNumber){
       case 1: return "Complete";
       case 2: return "Incomplete";
       case 3: return "Pending";
    }
  }  


const arrayWithStringStatus = arrayYouProvidedInTheQuestion
               .map( elem => 
               Object.assign(elem, {status: fromNumberToString(elem.status)})
               )