Sunny
Sunny

Reputation: 932

Create an array of objects from a nested object

I've a nested object 'empData'. I need to iterate over each of the keys in my object and return an array of objects. Each key inside my object 'empData' is again an object.

Now, I need to return an array of object as:

Expected output:

[{
 "team": "AUS",
 "name": "John"
}, {
 "team": "CAN",
 "name": "Steve"
}, {
 "team": "IND",
 "name": "Robbie"
}, {
 "team": "IRE",
 "name": "James"
}, {
 "team": "USA",
 "name": "Austin"
}];

My Code:

function getData() {
 const empData = {
  "AUS": {
   "isRetired": true,
   "name": "John"
  },
  "CAN": {
   "name": "Steve"
  },
  "IND": {
   "name": "Robbie"
  },
  "IRE": {
   "name": "James"
  },
  "USA": {
   "name": "Austin"
  }
 };
 Object.keys(empData).map(function(eachKey) {
  const obj = {
   team: eachKey,
   name: eachKey.name
  };
  console.log(obj);
  return obj;
 });
}
<button onclick="getData()">Get Data</button>

Could someone please help me with this?

Upvotes: 0

Views: 114

Answers (4)

brk
brk

Reputation: 50291

You can use for..in to iterate iver the object and create a new object with relevant keys and values and push to another array

function getData() {
  const empData = {
    "AUS": {
      "isRetired": true,
      "name": "John"
    },
    "CAN": {
      "name": "Steve"
    },
    "IND": {
      "name": "Robbie"
    },
    "IRE": {
      "name": "James"
    },
    "USA": {
      "name": "Austin"
    }
  };

  let newData = [];
  for (let keys in empData) {
    newData.push({
      team: keys,
      name: empData[keys].name
    })
  }
  console.log(newData)
}

getData()

Upvotes: 1

Devraj s
Devraj s

Reputation: 301

change name: eachKey.name

 name: empData[eachKey].name



function getData() {
 const empData = {
  "AUS": {
   "isRetired": true,
   "name": "John"
  },
  "CAN": {
   "name": "Steve"
  },
  "IND": {
   "name": "Robbie"
  },
  "IRE": {
   "name": "James"
  },
  "USA": {
   "name": "Austin"
  }
 };
 Object.keys(empData).map(function(eachKey) {
  const obj = {
   team: eachKey,
   name: empData[eachKey].name
  };
  console.log(obj);
  return obj;
 });
}

Upvotes: 4

CertainPerformance
CertainPerformance

Reputation: 370679

.map the Object.entries instead, so you can extract both the key (to get the team and the value (so you can get the name) at once:

const empData = {
  "AUS": {
    "isRetired": true,
    "name": "John"
  },
  "CAN": {
    "name": "Steve"
  },
  "IND": {
    "name": "Robbie"
  },
  "IRE": {
    "name": "James"
  },
  "USA": {
    "name": "Austin"
  }
};
const getData = () => {
  const result = Object.entries(empData).map(([team, { name }]) => ({
    team,
    name
  }));
  console.log(result);
};
<button onclick="getData()">Get Data</button>

Upvotes: 3

Eddie
Eddie

Reputation: 26844

You can use Object.entries to convert the object into an array. Use map to loop thru the array.

Object.entries first element is the key and the second is the value.

const empData = {
  "AUS": {
    "isRetired": true,
    "name": "John"
  },
  "CAN": {
    "name": "Steve"
  },
  "IND": {
    "name": "Robbie"
  },
  "IRE": {
    "name": "James"
  },
  "USA": {
    "name": "Austin"
  }
};

var result = Object.entries(empData).map(([team, {name}]) => ({team,name}));

console.log(result);

Upvotes: 2

Related Questions