-3

I have a array which i need to loop with map function and return object like a

{ 'acc' : anyValue } 
WillRasel
  • 3
  • 5
  • 1
    Read the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) how [`.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) works, and what arguments it passes to the callback. – Andreas Apr 13 '21 at 14:04
  • [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) -> [mcve] -> _'**Describe the problem**. "It doesn't work" isn't descriptive enough to help people understand your problem. Instead, tell other readers what the expected behavior should be. Tell other readers what the exact wording of the error message is, and which line of code is producing it.'_ – Andreas Apr 13 '21 at 14:05

2 Answers2

0

You can try the following way:

var arr = [
  {id: 1, name: "values 1" } ,
  {id: 1, name: "values 2" } 
];

arr = arr.map(({name}) => ({'acc' : name}));
console.log(arr);
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

You're currently not returning anything in your map callback function, therefore you get: [undefined undefined]

Try these instead:

arr.map((key, val) => ({ acc: val }) )
arr.map((key, val) => { return {acc: val} } )
ptts
  • 1,848
  • 6
  • 14