Is there any Array method similar to Array.prototype.map that takes an array and converts it into a key-value object?
Here's an example:
var people = [{name: 'person1', tableId: 1}, {name: 'person2', tableId: 2}];
var table = [{id:1, shape:2, title: 'table1'}, {id:2, shape:4, title: 'table2'}];
I would like to create a table-people object where the key will be an id and the value would be all the people that have the relevant tableId.
An example for the output will be:
var output = {
1: [{name: 'person1', tableId: 1}, <more people>],
2: [{name: 'person2', tableId: 2}}, <more people>]
};
output will be a key-value object. The key is the tableId and the value is an array of people. A person has some more information(other than name and tableId - this is just a simple example).
Now the easy way to do it is:
var output = {};
for(var i = 0 ; i < table.length ; i++)
{
output[table.id] = getPeopleThatSitInTable(table.id); // will return an array
}
Is there any method like:
table.keyValueMapObject(function(item){
return { key: item.id, value: getPeopleThatSitInTable(item.id) }
});
This method will create an object behind the scenes, fill it based on key & value and return the object?
I'm not looking for something like:
var obj = {}; // declaring an object
// loop somehow and fill obj(for\foreach\map\reduce etc.)
Since using for does the same thing in "the best way".
I'm looking for something like:
var obj = table.methodName(filterFunction)