Stefan Gies
Stefan Gies

Reputation: 318

How to convert a Java Hashmap to a javascript array properly

I want to convert a Java HashMap to a javascript array where the key is inside the javascript object.

I have tried to convert the JSON to a javascript object with an array inside. The Java Map looks like this < string: "sequence", Object: Round>

A quick overview of the objects I want to convert from a Java object (JSON) to javascript array.

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[];
}

The JSON I receive from the server:

[
  {
    "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": []
      }
    }
  }
]

Right now the thing I want to do is the following. I thought in javascript the order of a array is not guaranteed so I wanted to add the id inside the object after fetching it from the API.

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

I couldnt find a other solution like this in javascript, but hopefully you can give me a more proper solution:

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

Upvotes: 0

Views: 4117

Answers (2)

slim
slim

Reputation: 41203

The JSON you're receiving doesn't contain an array of Rounds; it contains a map, as you've observed. It would be much cleaner to look at whatever's serialising to this JSON, and correcting it so that the JSON looks like

[
  {
    ...
    "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": []
      }
    ]
  }
]

However if you really need to, you could do:

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

You said: "I thought in javascript the order of a array is not guaranteed". Arrays are definitely ordered in Javascript. You may be thinking of something that's not an array? Therefore I see little value in adding a sequence property to each member of the array.

If you really want to, though, it's simple enough:

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

This does all rely on the keys all being numbers, or coercable to numbers.

Upvotes: 1

wentjun
wentjun

Reputation: 42516

You can simplify it by making use of Object.values() to iterate through the values of order.rounds, followed by mapping the array to include the sequence property within each object of the array.

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

Upvotes: 1

Related Questions