How to create a new object from an existing one?

I do not know how to ask the question, but there is an array of view objects, for example:
[{id: 1, name: 'Vasya'}, {id: 'anyString', name: {}}]

How to make to make here such here new objects:
[{1: 'Vasya'}, {anyString: {}}]
June 10th 19 at 15:13
4 answers
June 10th 19 at 15:15
Solution
as well as the value of the object make a new object?
well, that is, the first object value = property of a new object, and the value is the second value of the source object. - maryse.Litt commented on June 10th 19 at 15:18
let newItem = {}
newItem[item.id] = item.name
- Isaias_Medhurst commented on June 10th 19 at 15:21
June 10th 19 at 15:17
Solution
var oldArr = [{id: 1, name: 'Vasya'}, {id: 'anyString', name: {}}]
var newArr = []
oldArr.forEach(function(item){ 
 var o = {}; 
 o[item.id] = item.name; 
 newArr.push( o ) 
})
June 10th 19 at 15:19
Solution
const oldData = [{id: 1, name: 'Vasya'}, {id: 'anyString', name: {}}];

const newData = oldData.map(function(item) {
 const newItem = {};
 newItem[item.id] = item.name;
 return newItem;
});


https://plnkr.co/edit/NhGeI9e4rxQQy0j5UUI5?p=preview
June 10th 19 at 15:21
Solution
var a = [{id: 1, name: 'Vasya'}, {id: 'anyString', name: {}}]

var b = a.map(obj => {
 let newObj = {}
 newObj[obj.id] = obj.name
 return newObj
})


https://jsfiddle.net/Lqwmtfud/
why map rather than forEach? - maryse.Litt commented on June 10th 19 at 15:24
, the same - Isaias_Medhurst commented on June 10th 19 at 15:27

Find more questions by tags JavaScript