格式化数据并删除密钥

时间:2019-06-08 21:53:34

标签: javascript arrays object ecmascript-6

我还有另一个数据问题。数据处理是我真正的弱点,因此尝试更好地理解它。我有一个名为data的变量,其中包含以下内容

[
    {"ID":1,"process":"quick","owner":"John","decission":"Admin","status":"failed","flag":"red"},
    {"ID":2,"process":"standard","owner":"Mary","decission":"Admin","status":"failed","flag":"blue"},
    {"ID":3,"process":"slow","owner":"John","decission":"Management","status":"active","flag":"red"}
]

它本质上是一个对象数组。我试图将其转换为一个数组,其中对象键是该数组的第一行,其内容如下。
但是,我不需要ID或标志。所以上面看起来像这样

[
    ["process","owner","decission","status"],
    ["quick","John","Admin","failed"],
    ["standard","Mary","Admin","failed"],
    ["slow","John","Management","active"]
]

所以要获得标题行,我正在考虑做这样的事情

const keys = Object.keys(this.data[0]);
keys.splice(keys.indexOf('ID'), 1);
keys.splice(keys.indexOf('flag'), 1);
this.formattedArray.push(keys);

对我来说,这似乎是一个非常分步的过程,然后如何确保其他数据位于下面的正确行中?

将原始数据格式化为现在所需的最佳方式是什么?

谢谢

4 个答案:

答案 0 :(得分:2)

首先获取密钥,然后获取基于这些密钥的值。

const data = [{"ID":1,"process":"quick","owner":"John","decission":"Admin","status":"failed","flag":"red"},{"ID":2,"process":"standard","owner":"Mary","decission":"Admin","status":"failed","flag":"blue"},{"ID":3,"process":"slow","owner":"John","decission":"Management","status":"active","flag":"red"}];
const keys = Object.keys(data[0]).filter(e => !(["ID", "flag"].includes(e)));
const values = data.map(e => Object.entries(e).filter(([k]) => keys.includes(k)).map(([, v]) => v));
const res = [keys, ...values];
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }

答案 1 :(得分:1)

由于我看到您没有所有标头的数组,因此可以使用以下方法创建它:

const headers = Object.keys(data[0]).filter(header => header !== 'ID');

在拥有所有标头之后,您可以继续提取数据:

const data = [
    {"ID":1,"process":"quick","owner":"John","decission":"Admin","status":"failed","flag":"red"},
    {"ID":2,"process":"standard","owner":"Mary","decission":"Admin","status":"failed","flag":"blue"},
    {"ID":3,"process":"slow","owner":"John","decission":"Management","status":"active","flag":"red"}
];

const headers = Object.keys(data[0]).filter(header => header !== 'ID');
const result = [headers];
data.forEach(row => result.push(headers.map(header => row[header])));

console.log(result);

答案 2 :(得分:1)

这是使用 Array.prototype.reduce

的另一种方法
const result = this.data.reduce((accumulator, currentValue) => {
    if (accumulator && accumulator.length === 0) {
        const headers = Object.keys(currentValue).filter((key) => key !== 'ID' && key !== 'flag');
        accumulator.push(headers);
    }
    accumulator.push(Object.values(currentValue).slice(1));
    return accumulator;
}, []);

答案 3 :(得分:1)

其他解决方案有效。我会做

const excludeItems = new Set(["ID", "flag"]);

function generateTableData(data) {
  const headers = Object.keys(data[0]).filter(item => !excludeItems.has(item));
  const items = data.map(item =>
    Object.entries(item).reduce((res, [key, value]) => {
      if (!excludeItems.has(key)) {
        res.push(value);
      }
      return res;
    }, [])
  );
  return [headers, ...items];
}

check it out here