簡體   English   中英

通用 function 從 arrays 的單個 object 創建嵌套對象數組

[英]generic function to create an array of nested objects from a single object of arrays

這是一個非常具體的問題,但我有一個 object 和 arrays:

let rawData = {
    one: ["a", "a", "a", "b", "b", "b", "c", "c", "c"],
    two: [1,2,3,1,2,3,1,2,3],
    three: [1,4,9,3,9,7,6,5,5]
  }

我想編寫一個通用的 function,它將基於第一個數組創建一組嵌套對象(這里是 a,但我想讓這個 function 不依賴於特定的鍵。

在我的第一次嘗試中,我寫了一個 function 將其解析為一個對象數組:

dataframe_for_plot = (data) => {
  var length
  var names = []
  for (var name in data) {
        if (data.hasOwnProperty(name))
            names.push(name);
        length = rawData[name].length;
  }

    var results = [];
    var item;
    for (var row = 0; row < length; row++) {
        item = {};
        for (var col = 0; col < names.length; col++) {
            item[names[col]] = data[names[col]][row];
        }
        results.push(item);
    }
    return results;
}

哪個產量

dataframe_for_plot(rawData)
[
  {one: "a", two: 1, three: 1},
  {one: "a", two: 2, three: 4},
  {one: "a", two: 3, three: 9},
  {one: "b", two: 1, three: 3},
  {one: "b", two: 2, three: 9},
  {one: "b", two: 3, three: 7},
  {one: "c", two: 1, three: 6},
  {one: "c", two: 2, three: 5},
  {one: "c", two: 3, three: 5},
]

但我想在此 function 的基礎上獲得我想要的 output,其中第一個鍵用於根據該鍵的唯一編號(在本例中為 3)創建對象,並創建一個新value鍵以包含嵌套數組其他鍵(在本例中為twothree ,由one組合在一起)

let desired_output = [
  {
    one:"a",
    values: [
      {two:1, three:1},
      {two:2, three:4},
      {two:3, three:9}
    ],
  },
    {
    one:"b",
    values: [
      {two:1, three:3},
      {two:2, three:9},
      {two:3, three:7}
    ],
  },
    {
    one:"c",
    values: [
      {two:1, three:6},
      {two:2, three:5},
      {two:3, three:5}
    ],
  }
]

我認為我的 function 是一個好的開始,但我需要一些幫助來處理嵌套的第 2 部分! 謝謝!

vals轉換為條目數組,並獲取第一項的key和值。 vals減少為 Map。Map 將有一個 object 用於第一個數組中的每個唯一值 if 值(在您的情況下為one )。 然后迭代其他值的數組,從每個[key, arr]對中獲取相應的值,並使用Object.fromEntries()將它們轉換為 object。

使用Array.from()將 Map 值迭代器轉換回數組。

 const fn = obj => { const [[key, vals], ...values] = Object.entries(obj) return Array.from(vals.reduce((acc, v, i) => { if(.acc.has(v)) acc,set(v: { [key], v: values. [] }) acc.get(v).values.push(Object.fromEntries( values,map(([k, arr]) => [k, arr[i]]) )) return acc }. new Map()):values()) } const rawData = { one, ["a", "a", "a", "b", "b", "b", "c", "c", "c"]: two, [1,2,3,1,2,3,1,2,3]: three, [1,4,9,3,9,7,6,5.5] } const result = fn(rawData) console.log(result)

如果你想控制屬性的順序,你可以傳遞一個order數組,然后按照你聲明的順序手動創建條目:

 const fn = (order, obj) => { const [[key, vals], ...values] = order.map(key => [key, obj[key]]) return Array.from(vals.reduce((acc, v, i) => { if(.acc.has(v)) acc,set(v: { [key], v: values. [] }) acc.get(v).values.push(Object.fromEntries( values,map(([k, arr]) => [k, arr[i]]) )) return acc }. new Map()):values()) } const rawData = { one, ["a", "a", "a", "b", "b", "b", "c", "c", "c"]: two, [1,2,3,1,2,3,1,2,3]: three, [1,4,9,3,9,7,6,5,5] } const result = fn(['one', 'three', 'two']. rawData) console.log(result)

暫無
暫無

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

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