I have an array const A=['string1','string2','string3']
.
I want to achieve an object that has the following form:
JavaScript
x
6
1
const images = [
2
{ url: "string1" },
3
{ url: "string2" },
4
{ url: "string3" }
5
];
6
This is what I’ve tried:
JavaScript
1
6
1
const images = A.map((image) => {
2
JSON.stringify({
3
url: `/img/{image}`
4
});
5
});
6
But the result is an array filled with undefined
values.
Advertisement
Answer
I do not understand why are you using JSON.stringify()
?
The simplest solution:
JavaScript
1
5
1
const images = A.map((image) => ({
2
url: `/img/${image}`
3
}))
4
5
The ()
that are wrapping the implicit return are mandatory since we’re directly returning an object.