将群集的obj转换为数组

时间:2018-11-03 17:15:15

标签: javascript

我想使用库markdown-table从我的对象中生成一个表。前提条件是创建具有一定结构的数组。

在我的最小可行示例下面查找:

const obj = {
  "2": {
    "title": "Product 1",
    "category": [{
      "cat_name": "Graphic Card"
    }],
    "currency": "$",
    "price": "513.40"
  },
  "3": {
    "title": "Product 2",
    "category": [{
      "cat_name": "Graphic Card"
    }],
    "currency": "$",
    "price": "599.00",
  },
  "4": {
    "title": "Memory Ram 1",
    "category": [{
      "cat_name": "Memory"
    }],
    "currency": "$",
    "price": "25.99",
  },
  "5": {
    "title": "Product 3",
    "category": [{
      "cat_name": "Graphic Card"
    }],
    "currency": "$",
    "price": "688.88",
  },
  "6": {
    "title": "Adapter 1",
    "category": [{
      "cat_name": "PCI-E"
    }],
    "currency": "$",
    "price": "48.99",
  }
}


var result = Object.keys(obj).map(function(key) {
  return [Number(key), obj[key]];
});

console.log(result)
<script src="https://raw.githubusercontent.com/wooorm/markdown-table/master/index.js"></script>

我的桌子得到以下结果:

| 2   | [object Object] |
| --- | --------------- |
| 3   | [object Object] |
| 4   | [object Object] |
| 5   | [object Object] |
| 6   | [object Object] |

但是,我尝试获取的结果数组应如下所示:

[
  ['Category', 'Model', 'Price'],
  ['Graphic Card', 'Prod 1', '$513.40'],
  ['Graphic Card', 'Product 2', '$599.00'],
  ['Graphic Card', 'Product 3', '$688.88'],
  ['Memory', 'Memory Ram 1', '$25.99'],
  ['PCI-E', 'Adapter 1', '$48.99']
]

有什么建议为什么不能正确解析我的数组并显示[object Object]

感谢您的答复!

2 个答案:

答案 0 :(得分:3)

[object Object]在对象上调用.toString()会得到什么。

看起来您需要显式地传递markdown-table您想要显示的值,因为它所做的只是在传递的内容上调用.toString()。我看不到它将在何处派生标头和值你。

类似这样的方法可能会解决问题:

let result = Object.keys(obj)
    .reduce((result, key) => {
        result.push([obj[key].category[0].cat_name, obj[key].title, obj[key].currency + obj[key].price]);
        return result;
    },
    [['Category', 'Model', 'Price']]
);

这里发生的是我在.reduce()中将标头作为累加器的初始值传递,并且每次迭代都将一行值压入累加的数组。

如果要对结果进行排序:

let result = Object.keys(obj)
    .sort((a, b) => {
        if (obj[a].category[0].cat_name < obj[b].category[0].cat_name)
            return -1;
        if (obj[a].category[0].cat_name > obj[b].category[0].cat_name)
            return 1;
        if (obj[a].model < obj[b].model)
            return -1;
        if (obj[a].model > obj[b].model)
            return 1;
        return 0;
    })
    .reduce(...

这是一个小提琴:https://jsfiddle.net/jmbldwn/t8Lvw2bj/2/

答案 1 :(得分:0)

您可以使用Object.values()从对象中获取值,然后将array#mapobject and array destructuring结合使用可以生成所需的数组。然后使用local#comparearray#sort,按字母顺序对数组进行排序。然后,您可以使用array#unshift['Category', 'Model', 'Price']添加到数组的开头。

const obj = { "2": {"title": "Product 1", "category": [{ "cat_name": "Graphic Card" }], "currency": "$", "price": "513.40" }, "3": { "title": "Product 2", "category": [{ "cat_name": "Graphic Card" }], "currency": "$", "price": "599.00", }, "4": { "title": "Memory Ram 1","category": [{ "cat_name": "Memory" }], "currency": "$", "price": "25.99", }, "5": { "title": "Product 3", "category": [{ "cat_name": "Graphic Card" }], "currency": "$", "price": "688.88", }, "6": { "title": "Adapter 1", "category": [{ "cat_name": "PCI-E"}], "currency": "$", "price": "48.99", } };
let result = Object
    .values(obj)
    .map(({title, category : [{cat_name}], currency, price}) => [cat_name, title, `${currency}${price}`])
    .sort((a,b) => a[0].localeCompare(b[0]) || a[1].localeCompare(b[1]));
result.unshift(['Category', 'Model', 'Price']);
console.log(result);