0

I want to create an object which will look like

{
    'Andy':{
        'Age' : 50,
        'Sport' : 'Football'
    },
    'Brenda':{
        'Age' : 33,
        'Sport' : 'Soccer'
    }
}

I'm trying to create this with JavaScript. I am able to get the strings via various api calls. My code looks like

const obj = {};
for (let i = 0; i < 2; i++) {
    const name = getName(i);
    const age = getAge(i);
    const sport = getSport(i);

    const newObj = {
            name:
            {
                'ageInYears' : age,
                'mainSport': sport
            }
        };

        obj+= newObj;  //doesn't work
        obj.push(newObj); //fails, probably because push is for an array
        obj.concat(newObj); // doesn't work
    }

Can any one explain what I need to do

Musa
  • 96,336
  • 17
  • 118
  • 137
MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120

4 Answers4

1

An object is a collection of key value pairs so you just assign a value to the key:

const obj = {};
for (let i = 0; i < 2; i++) {
    const name = getName(i);
    const age = getAge(i);
    const sport = getSport(i);

    obj[name] = {
        ageInYears: age,
        mainSport: sport
    };
}
Andrew Dibble
  • 782
  • 6
  • 15
1

You can do an Object.assign.

The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the target object. MDN Web Doc on Object.assign()

So Object.assign(obj,newObj) will push a new object with key name

0

To copy properties from one object into another, you can use the Object.assign() method.

let targetObject = {
  Andy: {  Age: 50, Sport: "Football" }
};
let sourceObject = {
  Brenda: { Age: 33, Sport: "Soccer" }
};

Object.assign(targetObject, sourceObject);

console.log(targetObject);
/*
{
  Andy: { Age: 50, Sport: 'Football' },
  Brenda: { Age: 33, Sport: 'Soccer' }
}
*/

Here is the original code, modified to work correctly:

// create code to match input data API
const names =['Andy', 'Brenda'];
const ages = [50, 33];
const sports = ['Football', 'Soccer'];
const getName = (i) => names[i];
const getSport = (i) => sports[i];
const getAge = (i) => ages[i];

const obj = {};

for (let i = 0; i < 2; i++) {
  const name = getName(i);
  const age = getAge(i);
  const sport = getSport(i);

  const newObj = {
    [name]: {
      "Age": age,
      "Sport": sport,
    },
  };

  Object.assign(obj, newObj);
}

console.log(obj);
/*
{
  Andy: { Age: 50, Sport: 'Football' },
  Brenda: { Age: 33, Sport: 'Soccer' }
}
*/
terrymorse
  • 6,771
  • 1
  • 21
  • 27
0

Here's one way:

[0, 1].reduce((obj, i) => (obj[getName(i)] = { Age: getAge(i), Sport: getSport(i) }, obj), {});

Working example:

const getName = i => ['Andy', 'Brenda'][i];
const getAge = i => [50, 33][i];
const getSport = i => ['Football', 'Soccer'][i];

var result = [0, 1].reduce((obj, i) => (obj[getName(i)] = { Age: getAge(i), Sport: getSport(i) }, obj), {});

console.log(result);
Wyck
  • 10,311
  • 6
  • 39
  • 60