【问题标题】:Is it possible to normalize "excel-like" JSON(without id's) with "normalizr"?是否可以用“normalizr”规范化“类似excel”的JSON(没有id)?
【发布时间】:2017-09-18 14:04:24
【问题描述】:

我想知道是否可以规范化这样的 JSON:

{
"rows": [{
        "cells": [{
                "value": "Column Name 1"
            },
            {
                "value": "Column Name 2"
            },
            {
                "value": "Column Name 3"
            },
            {
                "value": "Column Name 4"
            }
        ]
    },
    {
        "cells": [{
                "value": "Second Row Thing1"
            },
            {
                "value": "Second Row Thing2"
            },
            {
                "value": "Second Row Thing3"
            },
            {
                "value": "Second Row Thing4"
            }
        ]
    },
    {
        "cells": [{
                "value": "Third Row Thing1"
            },
            {
                "value": "Third Row Thing2"
            },
            {
                "value": "Third Row Thing3"
            },
            {
                "value": "Third Row Thing4"
            }
        ]
    }
]

}

变成这样漂亮的格式:

{
    "rows": [{
            "Column Name 1": "Second Row Thing1"
            "Column Name 2": "Second Row Thing1"
            "Column Name 3": "Second Row Thing1"
            "Column Name 4": "Second Row Thing1"
        },
        {
            "Column Name 1": "Third Row Thing1"
            "Column Name 2": "Third Row Thing1"
            "Column Name 3": "Third Row Thing1"
            "Column Name 4": "Third Row Thing1"
        }

    ]
}

基本上我想把第一行的数据当作列名来对待。然后将这些列名用作我的行对象中键的名称。可以用“normalizr”做这样的事情吗?还是我应该深入研究“map”、“foreach”等数组的东西? :)

【问题讨论】:

  • 请记住,对象属性没有定义的顺序。

标签: javascript arrays json object normalizr


【解决方案1】:

你真的不需要 Normalizr (而且你不应该使用它,因为它不起作用)。 Normalizr 用于嵌套数据:引用其他实体的实体(如推文中嵌入的用户)。

Map/Reduce 非常适合这样的事情。这是一个完全符合您要求的 sn-p。

const data = { "rows": [
  { "cells": [{ "value": "Column Name 1" }, { "value": "Column Name 2" }, { "value": "Column Name 3" }, { "value": "Column Name 4" } ]},
  { "cells": [{ "value": "Second Row Thing1" }, { "value": "Second Row Thing2" }, { "value": "Second Row Thing3" }, { "value": "Second Row Thing4" } ] },
  { "cells": [{ "value": "Third Row Thing1" }, { "value": "Third Row Thing2" }, { "value": "Third Row Thing3" }, { "value": "Third Row Thing4" } ] }
]};

// Get an array of the values of the first row
const columns = data.rows[0].cells.map(({ value }) => value);

// slice = take all rows except the first
// mapp each array of cells
const normalizedData = data.rows.slice(1).map(({ cells }) => {
  // reduce = converts the array of cells into an object map
  return cells.reduce((memo, { value }, i) => {
    memo[columns[i]] = value;
    return memo;
  }, {});
});

console.log(JSON.stringify(normalizedData, null, 2));

【讨论】:

    猜你喜欢
    • 2018-10-19
    • 2018-04-09
    • 2019-03-24
    • 2021-04-14
    • 2016-06-10
    • 2017-06-23
    • 2018-05-23
    • 2020-02-03
    • 2016-06-21
    相关资源
    最近更新 更多