0

I have an array like this:

[link1,link2,link3]

But I want it like this:

[uri:link1,uri:link2,uri:link3]

Please guide me over this. Thanks.

2
  • 2
    That's not a valid array in JavaScript. Why do you want it like that? Commented Mar 16, 2022 at 12:46
  • 3
    Since [uri:link1,uri:link2,uri:link3] is not valid JavaScript we don't really know what result you really want. So you want [{uri: link1}, {uri: link2}, ...] or {uri1: link1, uri2: link2, ...}? Having an array of objects with a single property is a bit unusual but of course might be necessary. Please provide more context about what you are actually trying to achieve. Commented Mar 16, 2022 at 12:46

3 Answers 3

2

maybe you could try by mapping your array and returning new array of object like this

const array1 = [1, 2, 3, 4];
const newArr = array1.map(i => {return {uri: i}})

console.log(newArr);
// > Array [Object { uri: 1 }, Object { uri: 2 }, Object { uri: 3 }, Object { uri: 4 }]
Sign up to request clarification or add additional context in comments.

Comments

1

If you need in the form of (Array of objects)

var test = ['a', 'b', 'c', 'd'];

function setID(item, index) {
  var fullname = {"id: ": item};
  return fullname;
}

var output = test.map(setID);
console.log(output);

output: [ { "id: ": "a" }, { "id: ": "b" }, { "id: ": "c" }, { "id: ": "d" } ]

2 Comments

OP didn't ask for an array of objects. There is not an object in sight. It is unclear what OP is actually asking.
ok , may be help others
0

Use Array.from! It's really simple and faster.

var test = ['a', 'b', 'c', 'd'];
var newTest = Array.from(test, val => 'id: '+ val);
console.log(newTest);

output: [ "id: a", "id: b", "id: c", "id: d" ]

Comments