Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript -- how to iterate through an array of objects to create a new object whose key is the value of the original object's initial key/value pair

Here is my situation:

I have to implement a function "mapById", which, when given an array of objects, returns an object, where the keys are the ids of the input objects and the values are the corresponding input objects.

var input = [{id: 102, name: "Alice"},
             {id: 205, name: "Bob", title: "Dr."},
             {id: 592, name: "Claire", age: 32}];

console.log(mapById(input));

should give the result below:

102: {id: 102, name: "Alice"},
205: {id: 205, name: "Bob", title: "Dr."},
592: {id: 592, name: "Claire", age: 32}

Here is my function thus far:

function mapById(list) {
    var obj = {};
    var objectKey = '';
    list.forEach(function(item) {
        objectKey = (Object.values(item)[0]);
        var obj = {[objectKey]: item};
    });
    return obj;
}

I can get a new key/value pair for each object in the original array but I can't figure out how to add each new key/value pair to the new object.

ObjectKey: 102
item: { id: 102, name: 'Alice' }
obj: { '102': { id: 102, name: 'Alice' } }
ObjectKey: 205
item: { id: 205, name: 'Bob', title: 'Dr.' }
obj: { '205': { id: 205, name: 'Bob', title: 'Dr.' } }
ObjectKey: 592
item: { id: 592, name: 'Claire', age: 32 }
obj: { '592': { id: 592, name: 'Claire', age: 32 } }

How can i fix this? If this was an array I could use the 'push' method. Is there a similar method for objects? Do I need a closure? I know this is basic stuff but I'm quite new to javascript.

Thanks.

like image 524
cocomatt Avatar asked Jan 27 '18 22:01

cocomatt


People also ask

How do you iterate through an array of key value pairs in JavaScript?

keys() method was introduced in ES6. It takes the object that you want to iterate over as an argument and returns an array containing all properties names (or keys). You can then use any of the array looping methods, such as forEach(), to iterate through the array and retrieve the value of each property.

How do you get the keys of an object in an array in JavaScript?

For getting all of the keys of an Object you can use Object. keys() . Object. keys() takes an object as an argument and returns an array of all the keys.

How do I iterate over an object key?

You have to pass the object you want to iterate, and the JavaScript Object. keys() method will return an array comprising all keys or property names. Then, you can iterate through that array and fetch the value of each property utilizing an array looping method such as the JavaScript forEach() loop.


2 Answers

You are actually quite close:

function mapById(list) {
    var obj = {};
    list.forEach(function(item) {
        obj[item.id] = item;
    });
    return obj;
}

How i would do that:

 const result = Object.assign(...input.map(el => ({[el.id] : el})));
like image 188
Jonas Wilms Avatar answered Sep 22 '22 04:09

Jonas Wilms


Use Array.prototype.reduce():

var input = [{id: 102, name: "Alice"},
             {id: 205, name: "Bob", title: "Dr."},
             {id: 592, name: "Claire", age: 32}]
             
var output = input.reduce(function(accumulator, item) {
  accumulator[item.id] = item
  return accumulator
}, {})

console.log(output)
             
like image 35
terales Avatar answered Sep 23 '22 04:09

terales