0

I have a JSON object like

{
"key1" : "value1",
"key2" : "value2",
}

i need to convert this value as Javascript array by combining both the values like

var my_array = ["value1 value2"]; //value 1 and value 2 of JSON

I tried:

var obj = JSON.parse(countrylist);
var countries = [obj.value1+ " " + obj.value2];
5
  • 2
    What have you done so far ? Commented Feb 28, 2020 at 10:47
  • 1
    I have updated the question. Please check it. Commented Feb 28, 2020 at 11:19
  • This is a typo. To access value1 from obj, you have to use obj.key1 Commented Feb 28, 2020 at 12:07
  • Note that there are no such things as a JSON object. JSONs are strings Commented Feb 28, 2020 at 12:08
  • just try this(if countrylist is a string and if first line given below exectues successfully you will get the output) var obj = JSON.parse(countrylist); var countries = [obj.key1+ " " + obj.key2]; Commented Feb 28, 2020 at 12:59

1 Answer 1

2

You can try this,

var obj = JSON.parse(countrylist);
var countries=Object.values(countrylist);

Here Object is a builtin javascript object.

To get the keys you can use Object.keys(your_object)

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

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.