简体   繁体   中英

How to change Json format like a value will be object name

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.",
  }
}

 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

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); 

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;
}, {});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM