0

I have array json elements inside a json. I need add orderCustomerCount in json Objects and assign a value for example 1.

the json is this.

    {
    "docs": [
        {
            "vehicle": "moto",
            "status": "confirmed",
            "_id": 34401,
            "service": "programmed"
        },
        {
            "vehicle": "moto",
            "status": "confirmed",
            "_id": 34402,
            "service": "programmed"
        }
    ]
}

my code is this

listOrders.docs.map((order)=>{
           order.orderCustomerCount = 1
            }) 

but when I return listOrders not show this new attribute orderCustomerCount.

2
  • 1. Array.map returns new array 2. Inside map callback you need to return something Commented Aug 30, 2021 at 5:05
  • Use this to add new property in your array listOrders.docs.map((order)=> ({...order, orderCustomerCount: 1 })). Commented Aug 30, 2021 at 5:08

2 Answers 2

2

The array map method returns a new array computed by calling the predicate in your case (order) => { order.orderCustomerCount; return undefined; } (verbose return, functions are equivalent) for each element of the array, and storing the return at the same position in a new array.

[1, 2, 3, 4, 5].map((n) => 2 * n);
// returns [2, 4, 6, 8, 10]

In your case, you return undefined in the predicate, because any arrow function has a default return of undefined.

In your case a more appropriate solution would be the array forEach method.

listOrders.docs.forEach((order)=> {
   order.orderCustomerCount = 1;
});

This method only calls the predicate for each element of the array. Because arrays and objects are reference types, the value of the elements are mutated to add the property.

Additionally, you might want to consider whether or whether not you want this. In my own experience, when using objects and arrays, in order to increase predictability, I rarely use assignments, instead I construct new arrays of new objects to ensure the source is not mutated.

let newOrders = listOrders.docs.map((order) => {
   let newOrder = {
       ...order, 
       // copy all attributes (properties) of order
       orderCustomerCount: 1,
       // add orderCustomerCount property equal to 1
   };

   return newOrder;
})

Note the spread syntax.

By doing this, the original object is kept as it were, and newOrders along with the elements of the array can be modified independently from it.

Sign up to request clarification or add additional context in comments.

Comments

1

the .map() function creates & returns a new array. You need to do the following:

listOrders.docs = listOrders.docs.map((order) => {
  order.orderCustomerCount = 1
  /* 
     NOTE: This line is important because if you don't return 
     anything from this function, then your array will have an 
     undefined item in it.
  */
  return order; 
}) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.