thisara wasugama
thisara wasugama

Reputation: 21

Issue with parsing the data into an integer JSON Array

I have a json file

{
    "Val1":120,
    "Val2":60,
    "Val3":50
}

I need to pass those into two Global Arrays.

1st Array = ["Val1","Val2","Val3"]
2nd Array = [120,60,50]

Upvotes: 1

Views: 40

Answers (2)

brk
brk

Reputation: 50291

You can use Object.keys & Object.values. Both of them will return array. Object.keys return array of keys and Object.values creates an array of values

let obj = {
  "Val1": 120,
  "Val2": 60,
  "Val3": 50
}

let array1 = Object.keys(obj);
let array2 = Object.values(obj);

console.log(array1, array2)

Upvotes: 3

KodeFor.Me
KodeFor.Me

Reputation: 13511

You could use a for loop like the one here:

var jsonObject = {
    "Val1":120,
    "Val2":60,
    "Val3":50
};

var arr1 = [];
var arr2 = [];

for(key in jsonObject) {
    arr1.push(key);
    arr2.push(jsonObject[key]);
}

console.log('Keys: ', arr1);
console.log('Values: ', arr2);

Upvotes: 1

Related Questions