簡體   English   中英

使用類似於Array.prototype.map()的方法將數組轉換為鍵值對象?

[英]Converting an array to a key-value object using a method similar to Array.prototype.map()?

是否有類似於 Array.prototype.map Array方法,它接受一個數組並將其轉換為鍵值對象?

這是一個例子:

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不以“最好的辦法”同樣的事情。

我正在尋找類似的東西:

var obj = table.methodName(filterFunction)

檢查_.groupBy()源代碼。 它完全符合你的要求。

我認為在這種情況下最相關的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.assign擴展運算符數組推導計算屬性名稱對象解構箭頭函數的強大功能,您可以按如下方式編寫它:

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

但它與第一​​個版本幾乎相同。 再次,這是低效的。 它目前似乎只適用於Firefox。

我想很快就不會有本土的等價物。

您可以使用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);

以下使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM