How to convert a Java Hashmap to a javascript array properly

Stefan Gies

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to convert the hashmap value in array in javascript?

From Dev

How to convert Array to HashMap in Java with Stream API

From Java

Java: how to convert HashMap<String, Object> to array

From Dev

JavaScript: Convert array of objects into hashmap

From Java

How to convert Array to HashMap using Java 8 Stream

From Dev

How to convert from Array to Hashmap

From Dev

how to properly convert byte array in java to char pointer in jni

From Java

How to convert String into Hashmap in java

From Dev

JavaScript: Convert array of objects into hashmap in Generic

From Dev

Convert array to Hashmap (key value pair) javascript

From Dev

Convert Java Linked Hashmap with linked Hashmap entries to array of objects

From Java

How to convert HashMap to json Array in android?

From Dev

how can i convert hashmap to array?

From Dev

Javascript: How to iterate array properly

From Dev

How to Properly Convert String to Associative Array in PHP

From Java

How to delete an array properly in Java

From Java

How to convert hashmap to JSON object in Java

From Dev

How to convert a stream to a HashMap (not Map) in Java 8

From Java

How does one convert a HashMap to a List in Java?

From Dev

How to convert following CSV to Hashmap using JAVA

From Dev

How to properly convert java object to Json (Nested)

From Dev

How to properly convert dto to json in Java?

From Dev

Convert javascript array to java object

From Dev

Convert Java List to Javascript Array

From Dev

Java: How to properly remove a value from a HashMap NOT based on the key

From Dev

how to convert items from a javascript array to java objects spring boot

From Dev

how to get the size of array inside a hashmap in java

From Dev

How to convert array to array of array in Javascript?

From Dev

Convert String to Hashmap in Java

Related Related

  1. 1

    How to convert the hashmap value in array in javascript?

  2. 2

    How to convert Array to HashMap in Java with Stream API

  3. 3

    Java: how to convert HashMap<String, Object> to array

  4. 4

    JavaScript: Convert array of objects into hashmap

  5. 5

    How to convert Array to HashMap using Java 8 Stream

  6. 6

    How to convert from Array to Hashmap

  7. 7

    how to properly convert byte array in java to char pointer in jni

  8. 8

    How to convert String into Hashmap in java

  9. 9

    JavaScript: Convert array of objects into hashmap in Generic

  10. 10

    Convert array to Hashmap (key value pair) javascript

  11. 11

    Convert Java Linked Hashmap with linked Hashmap entries to array of objects

  12. 12

    How to convert HashMap to json Array in android?

  13. 13

    how can i convert hashmap to array?

  14. 14

    Javascript: How to iterate array properly

  15. 15

    How to Properly Convert String to Associative Array in PHP

  16. 16

    How to delete an array properly in Java

  17. 17

    How to convert hashmap to JSON object in Java

  18. 18

    How to convert a stream to a HashMap (not Map) in Java 8

  19. 19

    How does one convert a HashMap to a List in Java?

  20. 20

    How to convert following CSV to Hashmap using JAVA

  21. 21

    How to properly convert java object to Json (Nested)

  22. 22

    How to properly convert dto to json in Java?

  23. 23

    Convert javascript array to java object

  24. 24

    Convert Java List to Javascript Array

  25. 25

    Java: How to properly remove a value from a HashMap NOT based on the key

  26. 26

    how to convert items from a javascript array to java objects spring boot

  27. 27

    how to get the size of array inside a hashmap in java

  28. 28

    How to convert array to array of array in Javascript?

  29. 29

    Convert String to Hashmap in Java

HotTag

Archive