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?

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
DrollDread
  • 321
  • 4
  • 22

1 Answers1

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);
cнŝdk
  • 31,391
  • 7
  • 56
  • 78