如何从波纹管形式设置json文件:我喜欢将id值设置为对象名称。

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


至以下表格:

{
  "1": {
    "public_name": "Duygu D.",
    "professions": "Kumas dizayn",
  },
  "2": {
    "public_name": "Meral A.",
    "professions": "Model Uyg.",
  }
}

最佳答案

您可以使用Array 方法。

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

关于javascript - 如何像值一样更改Json格式将是对象名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40976148/

10-11 23:52