1

suppose i have list something Like

let li = ["Agartala*","Belonia","Dharmanagar","Kailasahar","Khowai""Pratapgarh","Udaipur"]

i want convert it into JSON with key value like :

{ {"city" : "Agartala*"} ,
  {"city" : "Belonia"},
  {"city" : "Dharmanagar"}, 
  {"city" : "Kailasahar"}, 
  {"city" : "Khowai"},
    ....   }
3
  • 2
    li.map(city => ({city}}) Commented Mar 8, 2021 at 11:39
  • 1
    Does this answer your question? JS : Convert Array of Strings to Array of Objects Commented Mar 8, 2021 at 11:40
  • 1
    That's not a valid output? did you mean [ {...}, {...} ] instead? Commented Mar 8, 2021 at 11:42

1 Answer 1

1
var json = li.map(function (value, key) {
    return {
        "city": value,
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

(without JSON.stringify this function does not return JSON, it will return an array)

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.