gpt4 book ai didi

javascript - 使用 map/reduce 格式化数据集

转载 作者:行者123 更新时间:2023-11-30 15:39:42 28 4
gpt4 key购买 nike

下面是我的数据集和我想要的。 谁能告诉我如何将我的数据集格式化为我想要的格式? FIDDLE HERE (在浏览器中使用控制台)

我做了一个尝试(如下),这很接近但不是我想要的。

这是我的数据集

data_p = [
[{"key":"Device_Model","value":"test_model"},
{"key":">20MB/30","value":"11"},
{"key":">200MB/30","value":"33"},
{"key":">2048MB/30","value":"10"},
{"key":">5120MB/30","value":"55"},
{"key":">10240MB/30","value":"10"}
],
[{"key":"Device_Model","value":"0P6B670"},
{"key":">20MB/30","value":"9"},
{"key":">200MB/30","value":"8"},
{"key":">2048MB/30","value":"2"},
{"key":">5120MB/30","value":"23"},
{"key":">10240MB/30","value":"23"}]]

这就是我想要的

[{
"name": ">20MB/30",
"values": [{
"key": "test_model",
"value": 11
}, {
"key": "0P6B670",
"value": 9
}]
},

{
"name": ">200MB/30",
"values": [{
"key": "test_model",
"value": 33
}, {
"key": "0P6B670",
"value": 8
}]
},

{
"name": ">2048MB/30",
"values": [{
"key": "test_model",
"value": 10
}, {
"key": "0P6B670",
"value": 2
}]
},

{
"name": ">5120MB/30",
"values": [{
"key": "test_model",
"value": 55
}, {
"key": "0P6B670",
"value": 23
}]
},

{
"name": ">10240MB/30",
"values": [{
"key": "test_model",
"value": 10
}, {
"key": "0P6B670",
"value": 23
}]
}
]

这是我的尝试

bars = color.domain().map(function(name){
return {
name: name,
values: data_p.reduce(function(d) {
return d.slice(1)
})
}
});

它给了我这个:

"[{"name":">20MB/30","values":[{"key":">20MB/30","value":11},{"key":">200MB/30","value":33},{"key":">2048MB/30","value":10},{"key":">5120MB/30","value":55},{"key":">10240MB/30","value":10}]},{"name":">200MB/30","values":[{"key":">20MB/30","value":11},{"key":">200MB/30","value":33},{"key":">2048MB/30","value":10},{"key":">5120MB/30","value":55},{"key":">10240MB/30","value":10}]},{"name":">2048MB/30","values":[{"key":">20MB/30","value":11},{"key":">200MB/30","value":33},{"key":">2048MB/30","value":10},{"key":">5120MB/30","value":55},{"key":">10240MB/30","value":10}]},{"name":">5120MB/30","values":[{"key":">20MB/30","value":11},{"key":">200MB/30","value":33},{"key":">2048MB/30","value":10},{"key":">5120MB/30","value":55},{"key":">10240MB/30","value":10}]},{"name":">10240MB/30","values":[{"key":">20MB/30","value":11},{"key":">200MB/30","value":33},{"key":">2048MB/30","value":10},{"key":">5120MB/30","value":55},{"key":">10240MB/30","value":10}]}]"

注意:我在尝试中结合使用了 map、reduce 和 color,但这种方法可能是我的无知/经验不足。我不确定是否可以使用过滤器或任何其他功能。我对这些很熟悉,但没有我想要的那么多,所以对这些有一些建议会很好。

最佳答案

您可以使用对象作为哈希表并迭代数组。然后存储 Device_Model 并稍后将其用作键,而所有其他键和值转到结果集。

var data_p = [[{ key: "Device_Model", value: "test_model" }, { key: ">20MB/30", value: "11" }, { key: ">200MB/30", value: "33" }, { key: ">2048MB/30", value: "10" }, { key: ">5120MB/30", value: "55" }, { key: ">10240MB/30", value: "10" }], [{ key: "Device_Model", value: "0P6B670" }, { key: ">20MB/30", value: "9" }, { key: ">200MB/30", value: "8" }, { key: ">2048MB/30", value: "2" }, { key: ">5120MB/30", value: "23" }, { key: ">10240MB/30", value: "23" }]],
result = [];

data_p.forEach(function (a) {
var Device_Model;
a.forEach(function (b) {
if (b.key === 'Device_Model') {
Device_Model = b.value;
return;
}
if (!this[b.key]) {
this[b.key] = [];
result.push({ name: b.key, values: this[b.key] });
}
this[b.key].push({ key: Device_Model, value: b.value });
}, this);
}, Object.create(null));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 使用 map/reduce 格式化数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41003642/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com