-2

I might need some help on JavaScript about array stuff.

[
"URL_1",
"URL_2",
"URL_3",
"URL_4",
...,
"URL_30"
]

Let's just say these above are an array of strings which are links and how do I move them to an array object like this below?

[
  {
    url: "URL_1"
  },
  {
    url: "URL_2"
  },
  {
    url: "URL_3"
  },
...,
  {
    url: "URL_30"
  }
]
Reinhardt
  • 9
  • 2
  • 4
    `urls.map(url => ({ url }))` is a naive one-liner. – evolutionxbox Sep 21 '21 at 09:41
  • Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Sep 21 '21 at 10:02

2 Answers2

1

Using Array#map:

const arr = [ "URL_1", "URL_2", "URL_3", "URL_4", "URL_30" ];

const res = arr.map(url => ({ url }));

console.log(res);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
-1

simply:

    let temp = [];
    yourArray.foreach((val) =>{
    temp.push({val})
})
    yourArray = temp;
Dawood Ahmad
  • 476
  • 4
  • 13