1

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]
1
  • 3
    And what have you tried so far? It sounds like a simple problem: Array1 is Object.keys(o) and Array2 is Object.values(o)? Commented Feb 13, 2020 at 13:11

2 Answers 2

3

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Nice solution ! :)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.