user10045300
user10045300

Reputation: 427

adding a key to an array of objects in javascript

I am trying to add the key 'greeting' to an array of objects. My code is adding the key as expected but the only problem is that, it is adding another array at the bottom when I console log.

function greetDevelopers(list) {

list.greeting  = list.map(x => x.greeting = `Hi ${x.firstName}, what do 
you like most about ${x.language}?` );


console.log(list);


};

It is returning the following

[ { firstName: 'Sofia',
    lastName: 'I.',
    country: 'Argentina',
    continent: 'Americas',
    age: 35,
    language: 'Java',
    greeting: 'Hi Sofia, what do you like most about Java?' },
  { firstName: 'Lukas',
    lastName: 'X.',
    country: 'Croatia',
    continent: 'Europe',
    age: 35,
    language: 'Python',
    greeting: 'Hi Lukas, what do you like most about Python?' },
  { firstName: 'Madison',
    lastName: 'U.',
    country: 'United States',
    continent: 'Americas',
    age: 32,
    language: 'Ruby',
    greeting: 'Hi Madison, what do you like most about Ruby?' },
  greeting: [ 'Hi Sofia, what do you like most about Java?',
    'Hi Lukas, what do you like most about Python?',
    'Hi Madison, what do you like most about Ruby?' ] ]

Any suggestions on how to keep the greeting in each object but remove it from the end. would be greatly appreciated.

Thanks

Upvotes: 2

Views: 95

Answers (2)

brk
brk

Reputation: 50291

You need to update each object of the array and return a new array.In this list.greeting it is just creating a new key instead of the creating greeting key in each object.

let orgArray = [{
  firstName: 'Sofia',
  lastName: 'I.',
  country: 'Argentina',
  continent: 'Americas',
  age: 35,
  language: 'Java'
}, {
  firstName: 'Lukas',
  lastName: 'X.',
  country: 'Croatia',
  continent: 'Europe',
  age: 35,
  language: 'Python'
}, {
  firstName: 'Madison',
  lastName: 'U.',
  country: 'United States',
  continent: 'Americas',
  age: 32,
  language: 'Ruby'
}]


function greetDevelopers(list) {

  let newArr = list.map(function(x) {
    x.greeting = `Hi ${x.firstName}, what do you like most about ${x.language}?`
    return x;
  })
 return newArr;
};
console.log(greetDevelopers(orgArray))

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370679

You shouldn't be assigning to list.greeting - that assigns the result to the array (the new property that you see at the end of the array - arrays shouldn't have properties like that, only elements). What you want is just side-effects (not a new array), so you should use forEach instead of map (and don't assign the result to anything - simply log the array again):

const input = [ { firstName: 'Sofia',
    lastName: 'I.',
    country: 'Argentina',
    continent: 'Americas',
    age: 35,
    language: 'Java',
  }, { firstName: 'Lukas',
    lastName: 'X.',
    country: 'Croatia',
    continent: 'Europe',
    age: 35,
    language: 'Python',},
  { firstName: 'Madison',
    lastName: 'U.',
    country: 'United States',
    continent: 'Americas',
    age: 32,
    language: 'Ruby',
}];

function greetDevelopers(list) {
  list.forEach((item) => {
    item.greeting = `Hi ${item.firstName}, what do you like most about ${item.language}?`;
  });
  console.log(list);
}
greetDevelopers(input);

Upvotes: 3

Related Questions