0

This is my object:

{Mary : 'Engineer', Jane : 'Doctor'}

I want to turn it into this format:

{[Name: 'Marry', Occupation: 'Engineer'], [Name: 'Jane', Occupation: 'Doctor']}

How can I do it with javascript?

brainmassage
  • 1,234
  • 7
  • 23
  • 42
  • 1
    The syntax of your second example is wrong: it would have to be `[{name: 'Mary', occupation: 'Engineer'}, ...]` – ssube Nov 11 '15 at 16:30

8 Answers8

1

Just loop over the first object and place the elements where you want them.

var obj = {Mary : 'Engineer', Jane : 'Doctor'};
var people = [];

for(var name in obj){
    var occ = obj[name];

    people.push({Name: name, Occupation: occ});
}

The example object you show in your question uses invalid syntax. What you want is an array of objects, that's what this code does.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

You want to loop over the keys in your data and make a new object for each, with the name and occupation properties. Something like:

function makeNameOccupation(data) {
  return Object.keys(data).map(c => {
    return {name: c, occupation: data[c]};
  });
}

console.log(makeNameOccupation({Mary : 'Engineer', Jane : 'Doctor'}))

For each key in the original object (the person's name) you map that into an object with the name and occupation properties. Since you have the name (as the key), you can fetch the occupation from the original object with ease, and build the new record.

That can be minified, if you're into that sort of thing, into:

return Object.keys(data).map(c => ({name: c, occupation: data[c]}));
ssube
  • 47,010
  • 7
  • 103
  • 140
1
var result = [];
var i = "";
for (i in yourJSON) {
    // i is the key
    result.push({Name: i, Occupation: yourJSON[i]});
}

from How to access key itself using javascript

greetings

Community
  • 1
  • 1
messerbill
  • 5,499
  • 1
  • 27
  • 38
  • 1
    You should declare `i` as a `var` before you use it (you're using a global right now). – ssube Nov 11 '15 at 16:35
1

Try using Object.keys() , Array.prototype.map()

var data = {
  "Mary": 'Engineer',
  "Jane": 'Doctor'
}

var res = Object.keys(data).map(function(name) {
  return {
    "Name": name,
    "Occupation": data[name]
  }
});

console.log(JSON.stringify(res, null, 2))
guest271314
  • 1
  • 15
  • 104
  • 177
0

What you want isn't valid, you want an array of objects:

[{Name: 'Marry', Occupation: 'Engineer'}, {Name: 'Jane', Occupation: 'Doctor'}]

And you can do so like:

var people = []
var person = {Name: 'Jane', Occupation: 'Doctor'};
people.push(person);
tymeJV
  • 103,943
  • 14
  • 161
  • 157
0

So you have an object that looks like this

var originalPeopleObj = {Mary : 'Engineer', Jane : 'Doctor'}

What you want to do, is get the list of people's names inside your object.

var names = Object.keys(originalPeopleObj);

This will create an array of people names

names = ["Mary", "Jane"];

Now, you have to map those names to their occupations inside the originalPeopleObj

var newPeople = [];
for(var i = 0; i < names.length ; i++) {
     var peopleObj = {};
     peopleObj[i].name = names[i];
     peopleObj[i].occupation = originalPeopleObj[names[i]];
}
Divyanth Jayaraj
  • 950
  • 4
  • 12
  • 29
0

You can loop over the keys of the object and build a new array of objects.

var object = { Mary: 'Engineer', Jane: 'Doctor' },
    result = Object.keys(object).map(function (key) {
        return { Name: key, Occupation: object[key] };
    });
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

In case of es5, the key doesn't adapt the datatype automatically, the logic needs to be as follows: var object = { Mary: 'Engineer', Jane: 'Doctor' }; result = Object.keys(object).map(function (key) { return { Name: key, Occupation: object[key as keyof datatype] }; });

Naga - MS
  • 21
  • 2