如何将Java Hashmap正确转换为javascript数组

时间:2019-04-26 13:25:56

标签: javascript java hashmap

我想将Java HashMap转换为键位于javascript对象内部的javascript数组。

我试图将JSON转换为带有数组的javascript对象。 Java Map看起来像这样的<字符串:“ sequence”,对象:Round>

我要从Java对象(JSON)转换为javascript数组的对象的快速概述。

export class Order extends Base {
    public createdAt: Date;
    public status: OrderStatus;
    public rounds: Round[];
}
export class Round extends Base {
  public lockable: boolean;
  public locked: boolean;
  public sequence: number;
  public createdAt: string;
  public orderedItems?: Item[];
}

我从服务器收到的JSON:

[
  {
    "id": "87921b20-232d-4e13-8154-5ecdcdfeea83",
    "createdAt": 1556284343924,
    "status": "UNPAID",
    "rounds": {
      "1": {
        "id": "ffd791b7-3575-4653-9e65-0d95ed027d37",
        "createdAt": 1556284343930,
        "lockable": false,
        "locked": false,
        "orderedItems": []
      },
      "2": {
        "id": "310bbdff-2c41-4e22-96da-46282e158b49",
        "createdAt": 1556284343936,
        "lockable": true,
        "locked": false,
        "orderedItems": []
      }
    }
  }
]

现在我要做的是以下操作。我认为在javascript中不能保证数组的顺序,因此我想在从API中获取ID后在对象内部添加ID。

"2": <-- MOVE SEQUENCE inside the object like:
{
    ...
    "sequence": "2"
}

我在javascript中找不到其他这样的解决方案,但希望您能给我一个更合适的解决方案:

order.forEach(o => {
       const rounds = [];
       Object.keys(o.rounds).forEach(key => {
            o.rounds[key].sequence = key;
            rounds.push(o.rounds[key]);
       });
       o.rounds = rounds;
});

2 个答案:

答案 0 :(得分:1)

您收到的JSON不包含Round数组;如您所见,它包含一张地图。查看序列化到此JSON的任何内容,并进行更正以使JSON看起来更干净

[
  {
    ...
    "rounds": [
      {
        "id": "ffd791b7-3575-4653-9e65-0d95ed027d37",
        "createdAt": 1556284343930,
        "lockable": false,
        "locked": false,
        "orderedItems": []
      },
      {
        "id": "310bbdff-2c41-4e22-96da-46282e158b49",
        "createdAt": 1556284343936,
        "lockable": true,
        "locked": false,
        "orderedItems": []
      }
    ]
  }
]

但是,如果您确实需要,可以这样做:

var out = []
for(var key in obj) {
    out[key] = obj[key]
}

您说:“我认为在javascript中不能保证数组的顺序”。数组肯定是用Javascript排序的。您可能会想到不是数组的东西?因此,我没有为数组的每个成员添加sequence属性的价值。

但是,如果您真的想要,它就很简单:

var out = [];
for(var key in obj) {
    out[key] = m[key]
    out[key].sequence = key        
}

这完全取决于所有键都是数字或可强制转换为数字。

答案 1 :(得分:0)

您可以通过使用Object.values()来遍历order.rounds的值,然后将数组映射为在数组的每个对象中包含sequence属性来简化它。 / p>

const order = [{
  "id": "87921b20-232d-4e13-8154-5ecdcdfeea83",
  "createdAt": 1556284343924,
  "status": "UNPAID",
  "rounds": {
    "1": {
      "id": "ffd791b7-3575-4653-9e65-0d95ed027d37",
      "createdAt": 1556284343930,
      "lockable": false,
      "locked": false,
      "orderedItems": []
    },
    "2": {
      "id": "310bbdff-2c41-4e22-96da-46282e158b49",
      "createdAt": 1556284343936,
      "lockable": true,
      "locked": false,
      "orderedItems": []
    }
  }
}];

order.forEach(o => {
  const res = Object.values(o.rounds).map((object, index) => {
    object['sequence'] = index + 1;
    return object;
  });
  console.log(res)
});