-3

i have an array like this coming back response from the server:

[
    "111",
    "1010",
    "111",
    "1010",
    "1010"
]

i want to convert it into a JavaScript JSON like this:

[
 {
    "branch":  "111"

 },
 {
    "branch":  "1010"
 },
 {
    "branch":  "111"
 },
 {
    "branch":  "1010"
 },
 {
    "branch":  "1010"
 }
]
2
  • 2
    Possible duplicate of What is the concept of Array.map? Commented Feb 27, 2019 at 8:45
  • 1
    thats not a valid json.. -> check colons before } - I edited your question.. Commented Feb 27, 2019 at 8:51

2 Answers 2

3

You could map a short hand property.

var array = ["111", "1010", "111", "1010", "1010"],
    result = array.map(branch => ({ branch }));

console.log(result);

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

Comments

2

You can use map() to create a new array with the results of calling a provided function on every element in the calling array

var arr = [
    "111",
    "1010",
    "111",
    "1010",
    "1010"
]

var res = arr.map(i => ({'branch': i}));
console.log(res);

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.