joe
joe

Reputation: 59

How to add new value to array key

Can someone please help me with some code. I have an array and within the array there's status which hold different values from 0-3 but I want to convert the values to a proper meaning, so like the value 1 will be complete and 2 will be incomplete and 3 will be pending, So that when I display the details from the array on status instead of showing numbers it should show either complete or incomplete etc.

Here is my array:

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

Upvotes: 0

Views: 57

Answers (4)

Ele
Ele

Reputation: 33726

You can use the function Array.prototype.map to create a new array with the desired output.

Basically, this approach will create a clone of each object and will use an object "statuses" (a kind of mapping) to assign (Object.assign) the corresponding status as a string according to the object's status.

Important: this approach doesn't mutate the original array nor object in it.

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; }

Upvotes: 2

zizzo
zizzo

Reputation: 504

  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)})
               )

Upvotes: 0

Nitish Narang
Nitish Narang

Reputation: 4184

You can create a 'statusMap' object to define what values to map to like below and then loop (forEach) over the array

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)

Upvotes: 1

Ashish Ranjan
Ashish Ranjan

Reputation: 12960

You can loop over your array using forEach() and modify the status based on your requirement

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
    }

})

Upvotes: 0

Related Questions