Sorry, it is probably quite trivial, still I can't find a solution for:
I have an object that contains the following elements:
0: "A"
1: "B"
2: "C"
I would like to use the map() function to convert it to something like this:
0: {name: "A"}
1: {name: "B"}
2: {name: "C"}
If I use this:
this.xxx = this.operations.map(obj => obj.name);
console.log(this.xxx);
or this:
this.xxx = this.operations.map(obj => {name:obj} );
console.log(this.xxx);
the elements of xxx are undefined.
Sorry, it is probably quite trivial, still I can't find a solution for:
I have an object that contains the following elements:
0: "A"
1: "B"
2: "C"
I would like to use the map() function to convert it to something like this:
0: {name: "A"}
1: {name: "B"}
2: {name: "C"}
If I use this:
this.xxx = this.operations.map(obj => obj.name);
console.log(this.xxx);
or this:
this.xxx = this.operations.map(obj => {name:obj} );
console.log(this.xxx);
the elements of xxx are undefined.
Share Improve this question edited Apr 13, 2023 at 13:52 spender 120k35 gold badges244 silver badges366 bronze badges asked Jul 10, 2019 at 10:06 SanyifejűSanyifejű 2,74010 gold badges50 silver badges75 bronze badges 7-
3
Is it an object or an array that you have?
map
only works on arrays; for objects you'd need to use a workaround. (With indices like0
,1
and2
, it really should be an array.) – Amadan Commented Jul 10, 2019 at 10:09 -
3
this.operations.map(obj => {name:obj} )
looks like broken JS to me. If you want an arrow function to return a POJO, you need to wrap it in parentheses:this.operations.map(obj => ({name:obj}) )
– spender Commented Jul 10, 2019 at 10:09 - Possible duplicate of Iterate through object properties – Liam Commented Jul 10, 2019 at 10:09
-
That said what is
this
and what isthis.operations
? – Liam Commented Jul 10, 2019 at 10:10 -
object.values(operations).map(value => {name:value})
– Mosh Feu Commented Jul 10, 2019 at 10:10
7 Answers
Reset to default 10When you write
someArray.map(obj => {
//this is a code block, not an object definition
} )
the curly braces enclose a code block, not an object literal.
If you want an arrow function to return an object, JS requires you to wrap the object literal with parentheses in order to correctly parse your intent.
As such:
this.operations.map(obj => ({name: obj}) )
will fix your mapping.
Alternatively, if you want to do something more plex, use a codeblock and return the value:
this.operations.map(obj => {
// do some calculations
return {name: obj};
})
If I understood you would like to turn them to object? That's how you could do:
var x = ["A", "B", "C"];
console.log(x.map(obj => {return {name: obj}}));
Map object values to array of objects and then convert it into object using Object.assign
var obj = {
0: "A",
1: "B",
2: "C",
};
console.log(Object.assign({},Object.values(obj).map(a => ({ name: a }))));
First of all, if you have object, not sure how you can work with map function overall, since it's array prototype function. https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map however if its array you should try this:
const operations = [ "A", "B", "C"]
const xxx = operations.map(obj => ({name:obj}) );
console.log(xxx)
you were missing wrapping brackets, https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Advanced_syntax
but if its really an object then this should work (not sure about performance):
const operations = {
0: "A",
1: "B",
2: "C",
}
const xxx = {}
Object.entries(operations).forEach(entry => {
xxx[entry[0]] = { name: entry[1] }
});
console.log(xxx)
Try this,with Object.entries
let obj = {
0: "A",
1: "B",
2: "C"
}
let result = Object.entries(obj).map(([,name]) => ({
name
}));
console.log(result)
Since the object is similar to an array you can add the length
property making it array like
in this way. Then you can convert it to a real array using Array.from passing the transformation function as the second argument:
const input = {
0: "A",
1: "B",
2: "C"
}
const result = Array.from(
{ ...input, length: Object.keys(input).length },
item => ({ name: item })
)
console.log(result)
You can't use .map()
to produce an object, since it always returns an array.
Your best bet would be to get the entries in the object, then use .reduce()
on it, like so:
const operations = {
0: 'A',
1: 'B',
2: 'C',
}
const res = Object.entries(operations)
.reduce((acc, [key, val], i) => { acc[i] = { [key]: val }; return acc },{})
console.log(res)