1

This is my json:

[ { gsm: 'gsm',
  firstName: 'firstname',
  lastName: 'lastname' },
{ gsm: '123456789',
  firstName: 'Mohamed',
  lastName: 'Sameer'},
{ gsm: '987654321',
  firstName: 'Hameed',
  lastName: 'Basha'}]

I want my final output looks like:

[ { gsm: 'gsm',
  firstName: 'firstname',
  lastName: 'lastname',
  userKey: 'Key1'},
{ gsm: '123456789',
  firstName: 'Mohamed',
  lastName: 'Sameer',
  userKey: 'Key1'},
{ gsm: '987654321',
  firstName: 'Hameed',
  lastName: 'Basha',
  userKey: 'Key1'}]

i need to do this using any of the javascript methods like map...etc

  • Possible duplicate of [Add property to an array of objects](https://stackoverflow.com/questions/38922998/add-property-to-an-array-of-objects) – Niklesh Raut Jan 11 '18 at 07:16

5 Answers5

5

Use map

var output = arr.map( s => ( s.userKey = "Key1", s ) );

Demo

var arr = [{
    gsm: 'gsm',
    firstName: 'firstname',
    lastName: 'lastname'
  },
  {
    gsm: '123456789',
    firstName: 'Mohamed',
    lastName: 'Sameer'
  },
  {
    gsm: '987654321',
    firstName: 'Hameed',
    lastName: 'Basha'
  }
];

var output = arr.map( s => ( s.userKey = "Key1", s ) );

console.log( output );
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • 3
    This will mutate the original array `arr`. – Sebastian Simon Jan 11 '18 at 07:15
  • 2
    I think forEach is more appropriate in this case `arr.forEach( s => s.userKey = "Key1" );`. since none of the values in the array are updated, only the objects they reference are. – Paul Jan 11 '18 at 07:15
  • @Xufox If OP doesn't want the original array to be mutated, then Nina's answer is good enough. I was going to do the same thing but realized that Nina has done it already. – gurvinder372 Jan 11 '18 at 07:24
2

You could use Object.assign for generating a copy of the object and assign a new property and take Array#map for getting a new array.

var original = [ { gsm: 'gsm', firstName: 'firstname', lastName: 'lastname' }, { gsm: '123456789', firstName: 'Mohamed', lastName: 'Sameer' }, { gsm: '987654321', firstName: 'Hameed', lastName: 'Basha' }],
    copy = original.map(o => Object.assign({}, o, { key: 'key1' }));
    
console.log(copy);
console.log(original);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

In case you don't want to mutate the original array:

const arr = [ { gsm: 'gsm',
  firstName: 'firstname',
  lastName: 'lastname' },
{ gsm: '123456789',
  firstName: 'Mohamed',
  lastName: 'Sameer'},
{ gsm: '987654321',
  firstName: 'Hameed',
  lastName: 'Basha'}]
  
 const newArr = arr.map(item => ({
   ...item,
   userKey: 'Key1',
 }))
 
 console.log(newArr)
Hemerson Carlin
  • 7,354
  • 1
  • 27
  • 38
0
data.map(el => Object.assign(el, {userKey: 'Key1'}))

or

data.map(el => ({ ...el, userKey: 'Key1' }))
Pablo
  • 586
  • 5
  • 14
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation would greatly improve its long-term value](//meta.stackexchange.com/q/114762/350567) by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – iBug Jan 11 '18 at 08:38
0

You can do like this :)

var data = [ { gsm: 'gsm',
  firstName: 'firstname',
  lastName: 'lastname' },
{ gsm: '123456789',
  firstName: 'Mohamed',
  lastName: 'Sameer'},
{ gsm: '987654321',
  firstName: 'Hameed',
  lastName: 'Basha'}]
  
 var finalarray = data.map(x => (x.userKey = 'Key1', x));
 console.log(finalarray);
Mohamed Sameer
  • 2,998
  • 3
  • 22
  • 51