0

How to set json file from bellow form: I like to set id value to object name.

[{
  "id": 1,
  "public_name": "Duygu D.",
  "professions": "Kumas dizayn",
  "job_preference": "freelancer"
}, {
  "id": 2,
  "public_name": "Meral A.",
  "professions": "Model Uyg.",
  "job_preference": "freelancer"
}]

To below form:

{
  "1": {
    "public_name": "Duygu D.",
    "professions": "Kumas dizayn",
  },
  "2": {
    "public_name": "Meral A.",
    "professions": "Model Uyg.",
  }
}
Rajesh
  • 24,354
  • 5
  • 48
  • 79
Cappittall
  • 3,300
  • 3
  • 15
  • 23
  • Welcome to SO! Please refer to [How to ask](http://stackoverflow.com/help/how-to-ask) and provide necessary details – Rajesh Dec 05 '16 at 14:09
  • Possible duplicate of [How to convert array of Objects into one Object in JavaScript?](http://stackoverflow.com/questions/19874555/how-to-convert-array-of-objects-into-one-object-in-javascript) – Rajesh Dec 05 '16 at 14:19

4 Answers4

1

var arr = [{
    "id": 1,
    "public_name": "Duygu D.",
    "professions": "Kumas dizayn",
    "job_preference": "freelancer"
  }, {
    "id": 2,
    "public_name": "Meral A.",
    "professions": "Model Uyg.",
    "job_preference": "freelancer"
  }];

var newObj = {};

arr.forEach(function(el){
    newObj[el.id] = {public_name: el.public_name, professions: el.professions};
});

console.log(newObj);

newObj will be your wished object

ixpl0
  • 748
  • 5
  • 15
1

Iterate over your array and add a new property to the empty object result with the data you need

var data = [{
  "id": 1,
  "public_name": "Duygu D.",
  "professions": "Kumas dizayn",
  "job_preference": "freelancer"
}, {
  "id": 2,
  "public_name": "Meral A.",
  "professions": "Model Uyg.",
  "job_preference": "freelancer"
}];

var result = {};

data.forEach(function(item){
    result[item.id] = {
      "public_name" : item.public_name,
      "professions" : item.professions
      };
});

console.log(result);
Weedoze
  • 13,683
  • 1
  • 33
  • 63
1

You can use Array's reduce() method.

var arr = [{
  "id": 1,
  "public_name": "Duygu D.",
  "professions": "Kumas dizayn",
  "job_preference": "freelancer"
}, {
  "id": 2,
  "public_name": "Meral A.",
  "professions": "Model Uyg.",
  "job_preference": "freelancer"
}];

var obj = arr.reduce(function(o, v, i) {
  o[v.id] = {"public_name":v.public_name,  "professions": v.professions};
  return o;
}, {});
Tuhin
  • 3,335
  • 2
  • 16
  • 27
1

Try something like this:

// Your original array of objects stored in a variable.
const originalObject = [{
  "id": 1,
  "public_name": "Duygu D.",
  "professions": "Kumas dizayn",
  "job_preference": "freelancer"
}, {
  "id": 2,
  "public_name": "Meral A.",
  "professions": "Model Uyg.",
  "job_preference": "freelancer"
}];

// Create an empty new object to hold your new format.
const newObject = {};

// Loop the original object.
originalObject.forEach(item => {
  // Create a key in the new object based on the id of the original object.
  // Warning: This assumes your id's are unique.
  newObject[item.id] = { 
    public_name: item.public_name,
    professions: item.professions
  };
});

// Convert the object to a JSON string (optional).
const jsonString = JSON.stringify(newObject);

console.log(`As JS object ${newObject} or as JSON string ${jsonString}`);

This code is ES6, but can easily be converted to ES5.

Here's the example you can play with on Codepen:

http://codepen.io/anon/pen/WozoLo?editors=0010#0

Michael Giovanni Pumo
  • 14,338
  • 18
  • 91
  • 140