3

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.

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 like 0, 1 and 2, it really should be an array.) 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}) ) Commented Jul 10, 2019 at 10:09
  • Possible duplicate of Iterate through object properties Commented Jul 10, 2019 at 10:09
  • That said what is this and what is this.operations? Commented Jul 10, 2019 at 10:10
  • object.values(operations).map(value => {name:value}) Commented Jul 10, 2019 at 10:10

7 Answers 7

10

When 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 complex, use a codeblock and return the value:

this.operations.map(obj => {
    // do some calculations
    return {name: obj};
})
Sign up to request clarification or add additional context in comments.

2 Comments

somehow that ( ) I had missed... anyway. to add to that, we can enclose it in a multilline map(obj => { some code... return obj }) as well
@msanjay Added that alternative approach to my answer. Thx.
2

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}}));

1 Comment

Thanks and +1. But spender was quicker, therefore the accepted answer belongs to him.
1

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 }))));

1 Comment

Thanks and +1. But spender was quicker, therefore the accepted answer belongs to him.
1

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.org/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.org/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)

1 Comment

Thanks and +1. But spender was quicker, therefore the accepted answer belongs to him.
1

Try this,with Object.entries

let obj = {
  0: "A",
  1: "B",
  2: "C"
}

let result = Object.entries(obj).map(([,name]) => ({
  name
}));
console.log(result)

1 Comment

Thanks and +1. But spender was quicker, therefore the accepted answer belongs to him.
1

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)

1 Comment

Thanks and +1. But spender was quicker, therefore the accepted answer belongs to him.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.