1

I would like to convert a javascript array which looks like:

['https://www.google.com', 'https://www.facebook.com']

to a list of JSON objects that looks like this:

[{"redirectUri": "https://www.google.com"},
{"redirectUri": "https://www.facebook.com"}]

I have tried using Object.assign({}, array);

however this retuns json with the parameter name as the index of the array value and are all in a single object:

{"0": "https://www.google.com", "1": "https://www.facebook.com"},

is there a way to change this to use a custom parameter name dynamically?

1
  • 3
    array.map(redirectUri => ({ redirectUri }) ) Commented Dec 9, 2019 at 15:30

1 Answer 1

2

You just need to map your elements respectively, using Array.map() method:

let result = arr.map(o => {
  return {
    "redirectUri": o
  }
});

Demo:

let arr = ['https://www.google.com', 'https://www.facebook.com'];

let result = arr.map(o => {
  return {
    "redirectUri": o
  }
});
console.log(result);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.