使用类似于Array.prototype.map()的方法将数组转换为键值对象?

时间:2014-12-20 06:18:47

标签: javascript arrays

是否有Array方法类似Array.prototype.map采用数组并将其转换为键值对象?

以下是一个例子:

var people = [{name: 'person1', tableId: 1}, {name: 'person2', tableId: 2}];
var table = [{id:1, shape:2, title: 'table1'}, {id:2, shape:4, title: 'table2'}];

我想创建一个table-people对象,其中键将是一个id,值将是所有具有相关tableId的人。

输出的示例如下:

var output = {
  1: [{name: 'person1', tableId: 1}, <more people>], 
  2: [{name: 'person2', tableId: 2}}, <more people>]
};

output将成为键值对象。关键是tableId,值是一组人。一个人有更多的信息(除了name和tableId - 这只是一个简单的例子)。

现在,简单的方法是:

var output = {};
for(var i = 0 ; i < table.length ; i++)
{
   output[table.id] = getPeopleThatSitInTable(table.id); // will return an array
}

有没有像:

这样的方法
table.keyValueMapObject(function(item){
  return { key: item.id, value: getPeopleThatSitInTable(item.id) }
});

此方法将在幕后创建一个对象,并根据键和键填充它。值和返回对象?

寻找类似的内容:

var obj = {}; // declaring an object
// loop somehow and fill obj(for\foreach\map\reduce etc.)

由于使用for在&#34;中做同样的事情,最好的方式&#34;。

寻找之类的内容:

var obj = table.methodName(filterFunction)

4 个答案:

答案 0 :(得分:2)

检查_.groupBy()来源。它完全符合您的要求。

答案 1 :(得分:1)

我认为在这种情况下最相关的Array方法是reduce。虽然它没有多大帮助,但您将表格缩减为键值对象:

var result = table.reduce(function(result, entry) {
    result[entry.id] = people.filter(function(person) { // pretty inefficient
        return person.tableId === entry.id;
    });
    return result;
}, {}); // initial key-value object

但实际上这种reduce的使用有点荒谬,因为回调并没有真正结合两个值。它只是修改和传递初始对象。虽然它使意图明确,但与使用forEach相同:

var result = {}; // initial key-value object
table.forEach(function(entry) {
    result[entry.id] = people.filter(function(person) { // pretty inefficient
        return person.tableId === entry.id;
    });
});

即使这样过滤整个people数组也是非常低效的。你应该只迭代一次。

var result = {}; // initial key-value object
table.forEach(function(entry) {
    result[entry.id] = [];
});
people.forEach(function(person) {
    result[person.tableId].push(person);
});

我认为没有太多简化的可能性。

当然,凭借Object.assignspread operatorarray comprehensionscomputed property namesobject destructuringarrow functions的强大功能,您可以将其写为如下:

var result = Object.assign({}, ...[{
    [id]: people.filter(
        ({tableId}) => tableId === id
    )
} for({id} of table)]);

但它与第一​​个版本几乎相同。它再次效率低下。它目前似乎只适用于Firefox。

我想很快就不会成为本地人。

答案 2 :(得分:0)

您可以使用forEach来迭代表并通过基于tableId过滤人员来构建table-people对象,如下所示,

"use strict";

var people = [{name: 'person1', tableId: 1}, {name: 'person2', tableId: 2}];
var table = [{id:1, shape:2, title: 'table1'}, {id:2, shape:4, title: 'table2'}];

var tablePeopleObj = {};
table.forEach(function (tableItem) {
  tablePeopleObj[tableItem.id] = getPeople(tableItem.id);
});


function getPeople(tableId) {
  return people.filter(function function_name (person) {
    return person.tableId == tableId;
  });
}

console.log(tablePeopleObj);

答案 3 :(得分:0)

以下使用 Array.prototype.map ,但可能不是您想要的。考虑它只是一个可能有用的使用示例:

var people = [{name: 'person1', tableId: 1}, {name: 'person2', tableId: 2}];
var obj = {};

people.map(function(v, i) {
  var key = v.tableId;
  if (!this.hasOwnProperty(key)) {
    this[key] = [];
  }
  this[key].push(v);
}, obj);