admin管理员组

文章数量:1296490

I want to change my json structure, how can I do it?

im getting a json that looks like this:

 body: {
     "111111": {
         "name": "exp1",
         "status": 10000
     },
     "222222": {
         "name": "exp2",
         "status": 20000
     },
     "333333": {
         "name": "exp3",
         "status": 30000
     }
 }

but I need it in this structure:

 body: {
     bulks: [{
         "id": "111111",
         "name": "exp1",
         "status": 100000
     }, {
         "id": "222222",
         "name": "exp2",
         "status": 200000
     }, {
         "id": "333333",
         "name": "exp3",
         "status": 300000
     }]
 }

Cause in my html I want to read it like this:

<div *ngIf="showingList">
  <div class="list-bg"  *ngFor="#bulk of listBulks | async">
    ID: {{bulk.id}} name of item: {{bulk.name}}
  </div>
</div>

I want to change my json structure, how can I do it?

im getting a json that looks like this:

 body: {
     "111111": {
         "name": "exp1",
         "status": 10000
     },
     "222222": {
         "name": "exp2",
         "status": 20000
     },
     "333333": {
         "name": "exp3",
         "status": 30000
     }
 }

but I need it in this structure:

 body: {
     bulks: [{
         "id": "111111",
         "name": "exp1",
         "status": 100000
     }, {
         "id": "222222",
         "name": "exp2",
         "status": 200000
     }, {
         "id": "333333",
         "name": "exp3",
         "status": 300000
     }]
 }

Cause in my html I want to read it like this:

<div *ngIf="showingList">
  <div class="list-bg"  *ngFor="#bulk of listBulks | async">
    ID: {{bulk.id}} name of item: {{bulk.name}}
  </div>
</div>
Share Improve this question edited Jul 25, 2016 at 12:04 Andreas 21.9k7 gold badges51 silver badges58 bronze badges asked Jul 25, 2016 at 11:59 jack miaojack miao 1,4983 gold badges18 silver badges37 bronze badges 4
  • What have you tried? The conversion is a simple wrap are you sure you have the data correct? var listBulks = {body: {bulks: [oldBodyList]}}; – Sukima Commented Jul 25, 2016 at 12:03
  • @Sukima where do I add this conversion you suggested? I didnt try anything yet I dont know what to try... – jack miao Commented Jul 25, 2016 at 12:05
  • Where ever you put your JavaScript. Doesn't Angular have JavaScript? That the best I can do. Maybe someone else who uses Angular knows more. – Sukima Commented Jul 25, 2016 at 12:06
  • Why convert it? docs.angularjs/api/ng/directive/ngRepeat – epascarello Commented Jul 25, 2016 at 12:08
Add a ment  | 

3 Answers 3

Reset to default 4

Using Object#entries and Array#map with spread operator.

const data={body:{111111:{name:"exp1",status:1e4},222222:{name:"exp2",status:2e4},333333:{name:"exp3",status:3e4}}};


const res = {body:{bulk:Object
.entries(data.body)
.map(a=>({id: a[0], ...a[1]}))}};

console.log(res);

You can do it using reduce:

var body = {
    "111111": {
        "name": "exp1",
        "status": 10000
    },
    "222222": {
        "name": "exp2",
        "status": 20000
    },
    "333333": {
        "name": "exp3",
        "status": 30000
    }
}

var bodyArray = Object.keys(body).reduce(function(result, key) {
    var item = body[key];
    item.id = key;
    result.push(item)
    return result;
}, []);

As a simplest alternative to reduce, you could use the map() function.

const body = {
  "111111": {
    "name": "exp1",
    "status": 10000
  },
  "222222": {
    "name": "exp2",
    "status": 20000
  },
  "333333": {
    "name": "exp3",
    "status": 30000
  }
}

const newArray = Object.keys(body).map(function(key) {
  const newObject = {
    id: key,
    ...body[key]
  };
  return newObject;
});

console.log(newArray);

本文标签: javascriptHow to change json structure to look like another json structureStack Overflow