Home > Mobile >  How to insert value from an array to an array of object [duplicate]
How to insert value from an array to an array of object [duplicate]

Time:09-22

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"
  }
]

CodePudding user response:

Using Array#map:

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

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

console.log(res);

CodePudding user response:

simply:

    let temp = [];
    yourArray.foreach((val) =>{
    temp.push({val})
})
    yourArray = temp;
  • Related