在 Javascript 对象中动态交换 key/values
Dynamic swapping of key/values in a Javascript object
我即将在分组直方图中显示我从数据库中聚合的数据。
数据如下所示:
[
{
"_id": "Gas Separator Filter",
"sellingPrice": 100000,
"quantity": 10
},
{
"_id": "Dry Gas Cartridge",
"sellingPrice": 6005000,
"quantity": 15
}
]
但是为了在图表中显示它并将它们分组,我需要这样的东西。对于上面数据集中的每个 _id
,我应该能够在图表中看到两个条。
[
{
"name": "quantity",
"Gas Separator Filter": 10,
"Dry Gas Cartridge": 15
},
{
"name": "sellingPrice",
"Gas Separator Filter": 100000,
"Dry Gas Cartridge": 6005000
}
]
已经两个小时了,我想不出一个好的方法来做这件事。你会建议什么?
您可以使用 array.reduce 来实现:
const arrayToArray = (array) => {
var ret = [{
"name": "price"
}, {
"name": "quantity"
}
];
return array.reduce((obj, item, idx, original) => {
ret[0][item._id] = original[idx].sellingPrice;
ret[1][item._id] = original[idx].quantity;
return ret;
}, 0)
}
像这样,您可以使用基础对象设置一个变量,您可以用一对 _id:value 来填充价格和数量。
但这不是 "elegant" 的方法。您确定需要此对象数组结构来显示图表吗?
我发现很难解释我的解决方案,但这是我的看法(您可以为不同的变量添加 console.log
s 以遵循转换):
提取对象中的所有键,循环遍历它们,name
就是那个键,并使用嵌套循环设置其他键和值:
const data = [ { "_id": "Gas Separator Filter", "sellingPrice": 100000, "quantity": 10 }, { "_id": "Dry Gas Cartridge", "sellingPrice": 6005000, "quantity": 15 } ]
const [_id, ...keys] = [...new Set(data.map(e => Object.keys(e)).flat())]
// console.log(keys) : ["sellingPrice", "quantity"]
const result = keys.map(key => {
const values = data.map(obj => ({
[obj._id]: obj[key]
}))
return Object.assign({
name: key
}, ...values);
})
console.log(result)
这是一个使用老派 for 循环的解决方案:)
const transform = (data, nameValues, keyProp) => {
const result = [];
for (const name of nameValues) {
const output = { name };
for (const value of data) {
output[value[keyProp]] = value[name];
}
result.push(output);
}
return result;
};
const result = transform(
[
{
"_id": "Gas Separator Filter",
"sellingPrice": 100000,
"quantity": 10
},
{
"_id": "Dry Gas Cartridge",
"sellingPrice": 6005000,
"quantity": 15
}
],
["sellingPrice", "quantity"],
"_id"
);
console.log(result);
我即将在分组直方图中显示我从数据库中聚合的数据。
数据如下所示:
[
{
"_id": "Gas Separator Filter",
"sellingPrice": 100000,
"quantity": 10
},
{
"_id": "Dry Gas Cartridge",
"sellingPrice": 6005000,
"quantity": 15
}
]
但是为了在图表中显示它并将它们分组,我需要这样的东西。对于上面数据集中的每个 _id
,我应该能够在图表中看到两个条。
[
{
"name": "quantity",
"Gas Separator Filter": 10,
"Dry Gas Cartridge": 15
},
{
"name": "sellingPrice",
"Gas Separator Filter": 100000,
"Dry Gas Cartridge": 6005000
}
]
已经两个小时了,我想不出一个好的方法来做这件事。你会建议什么?
您可以使用 array.reduce 来实现:
const arrayToArray = (array) => {
var ret = [{
"name": "price"
}, {
"name": "quantity"
}
];
return array.reduce((obj, item, idx, original) => {
ret[0][item._id] = original[idx].sellingPrice;
ret[1][item._id] = original[idx].quantity;
return ret;
}, 0)
}
像这样,您可以使用基础对象设置一个变量,您可以用一对 _id:value 来填充价格和数量。 但这不是 "elegant" 的方法。您确定需要此对象数组结构来显示图表吗?
我发现很难解释我的解决方案,但这是我的看法(您可以为不同的变量添加 console.log
s 以遵循转换):
提取对象中的所有键,循环遍历它们,name
就是那个键,并使用嵌套循环设置其他键和值:
const data = [ { "_id": "Gas Separator Filter", "sellingPrice": 100000, "quantity": 10 }, { "_id": "Dry Gas Cartridge", "sellingPrice": 6005000, "quantity": 15 } ]
const [_id, ...keys] = [...new Set(data.map(e => Object.keys(e)).flat())]
// console.log(keys) : ["sellingPrice", "quantity"]
const result = keys.map(key => {
const values = data.map(obj => ({
[obj._id]: obj[key]
}))
return Object.assign({
name: key
}, ...values);
})
console.log(result)
这是一个使用老派 for 循环的解决方案:)
const transform = (data, nameValues, keyProp) => {
const result = [];
for (const name of nameValues) {
const output = { name };
for (const value of data) {
output[value[keyProp]] = value[name];
}
result.push(output);
}
return result;
};
const result = transform(
[
{
"_id": "Gas Separator Filter",
"sellingPrice": 100000,
"quantity": 10
},
{
"_id": "Dry Gas Cartridge",
"sellingPrice": 6005000,
"quantity": 15
}
],
["sellingPrice", "quantity"],
"_id"
);
console.log(result);