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

1

5 Answers 5

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

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

3 Comments

This will mutate the original array arr.
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.
@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.
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; }

Comments

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)

Comments

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

1 Comment

he is putting with snippet
0
data.map(el => Object.assign(el, {userKey: 'Key1'}))

or

data.map(el => ({ ...el, userKey: 'Key1' }))

1 Comment

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value 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.

Your Answer

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