0

I have a array of javascript objects

var arr =  [ 
        { 
          query_result_id: 25,
          author_email: '[email protected]'
        },
       { 
          query_result_id: 28,
          author_email: '[email protected]'
        },
  ]

I am using .map to enter new values on each javascript object

arr
        .map( s => s["status"] = "dev")
        .map( s => s["customer_id"] = customerId)
        .map( s => s["email_nb"] = emailId)
        //and so on for about 10 new key/values

the output is:

 var arr =  [ 
            { 
              query_result_id: 25,
              author_email: '[email protected]',
              status: dev,
              customer_id: 45,
              email_nb: 45
            },
           { 
              query_result_id: 28,
              author_email: '[email protected]',
              status: dev,
              customer_id: 78,
              email_nb: 56
            },
      ]

Is it possible in javascript to not chain 10 .map but instead one single operation to make it cleaner and maybe even more performant/faster ?

4
  • Just add all object to s at once and return it later. Commented Sep 25, 2019 at 10:26
  • sorry but how do you do this ? Commented Sep 25, 2019 at 10:27
  • Where is the data that you are adding? Is it also an array of objects? Commented Sep 25, 2019 at 10:28
  • arr = arr.map(s => { s.status = 'a'; s.customer_id = 'b'; return s; }); Commented Sep 25, 2019 at 10:29

4 Answers 4

3

when you intend to keep the same values inside the array you shouldn't use map.

map is intended to recreate a new array with values depending on source array. What you want to do is to apply a function on each element that's what forEach is made for

here's how you can do it using foreach :

let customerId = "customerId"
let emailId = "emailId"

var arr =  [{query_result_id: 25,author_email: '[email protected]'},
            {query_result_id: 28,author_email: '[email protected]'}]

arr.forEach( s => {
  s["status"] = "dev"
  s["customer_id"] = customerId
  s["email_nb"] = emailId
})
        
console.log(arr)

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

2 Comments

Although other answers suggest .map is possible i understand the underlying concept better now of .map ot change sth and .forEach to add new elements. thanks
forEach is not to add new element, it is to loop over the element without changing the array itself (here since the elements are object we can modify them without touching the array)
0

Yes it is possible, for example like this:

arr.map(s => {
         s["status"] = "dev";
         s["customer_id"] = customerId;
         s["email_nb"] = emailId;

         return s;
    });

Always keep in mind, that this:

arr.map(x => x + 1);

Is just a shorthand for this:

arr.map(x => {
   return x + 1;
});

Comments

0

Also, you don't have to chain map for every change you are making to your data. Do all of the all of the changes in the body of the map's callback, and then return the object.

let data = arr.map(entry => {
    entry["something"] = otherData["something"];
    entry["something else"] = otherData["something-else"];
    // etc. for all of the keys
    // now the entry is entirely transformed, you can return it
    return entry;
})

Comments

0

For each object, you can create a new object by re-using the old one's properties/values using the spread operator, and adding the other properties/values to this new object literal:

arr = arr.map(s => (
  {...s, status : "dev", customer_id : customerId, email_nb : emailId}
));

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.