将对象数组转换为特定格式?
convert an array of objects into a specific format?
我有以下一组数据,它们采用数组中对象的格式。
const columns = [
{
"usage": 395226,
"population": 1925117,
"value": 21,
"abv": "MN"
},
{
"usage": 893129,
"population": 4327541,
"value": 21,
"abv": "IL"
}
]
我需要将上面的一组数据转换成下面显示的格式。我被困在这里有一段时间了。我尝试编写一个函数将其转换为所需的数据格式,但没有成功。
let columns = [
["abv", "population"],
["MN", 1925117],
["IL", 4327541]
]
如果有人能帮我解决这个问题,那将是一个很大的帮助。
提前致谢:)
这能解决您的问题吗?
const columns = [
{
"usage": 395226,
"population": 1925117,
"value": 21,
"abv": "MN"
},
{
"usage": 893129,
"population": 4327541,
"value": 21,
"abv": "IL"
}
]
var k = [["abv", "population"]];
for(var i = 0; i < columns.length; i++){
k.push([columns[i].abv, columns[i].population]);
}
console.log(k) // Expected expected output
如果您需要,这里有一个通用的实现。
var names = ["abv", "population"];
var k = GetPropertiesInArray(columns, names); //[["abv", "population"]];
function GetPropertiesInArray(columns, names){
var m = [];
m.push(names);
for(var i = 0; i < columns.length; i++){
var item = [];
for(var j in names){
item.push(columns[i][names[j]]);
}
m.push(item);
}
return m;
}
console.log(k);//Expected output
我会用减速器来做:
const cols = [
{
"usage": 395226,
"population": 1925117,
"value": 21,
"abv": "MN"
},
{
"usage": 893129,
"population": 4327541,
"value": 21,
"abv": "IL"
}
]
cols.reduce(function(acc, current) {
acc.push([current.abv, current.population])
return acc;
}, ["abv", "population"]);
或者如果您不喜欢使用 push
:
cols.reduce(function(acc, current) {
acc = [
...acc,
[current.abv, current.population]
]
return acc;
}, ["abv", "population"]);
您的第一个数据在所需输出中似乎是静态的:
const columns = [
{
"usage": 395226,
"population": 1925117,
"value": 21,
"abv": "MN"
},
{
"usage": 893129,
"population": 4327541,
"value": 21,
"abv": "IL"
}
];
let cols = [];
if (columns.length) {
cols.push(["abv", "population"]);
columns.forEach(item => {
cols.push([item.abv, item.population])
});
}
console.log(cols);
我最终会使用这样的东西:
const columns = [
{
"usage": 395226,
"population": 1925117,
"value": 21,
"abv": "MN"
},
{
"usage": 893129,
"population": 4327541,
"value": 21,
"abv": "IL"
}
];
let converted = columns.map((c) => [c.abv, c.population]);
converted.unshift(["abv", "population"]);
我有以下一组数据,它们采用数组中对象的格式。
const columns = [
{
"usage": 395226,
"population": 1925117,
"value": 21,
"abv": "MN"
},
{
"usage": 893129,
"population": 4327541,
"value": 21,
"abv": "IL"
}
]
我需要将上面的一组数据转换成下面显示的格式。我被困在这里有一段时间了。我尝试编写一个函数将其转换为所需的数据格式,但没有成功。
let columns = [
["abv", "population"],
["MN", 1925117],
["IL", 4327541]
]
如果有人能帮我解决这个问题,那将是一个很大的帮助。 提前致谢:)
这能解决您的问题吗?
const columns = [
{
"usage": 395226,
"population": 1925117,
"value": 21,
"abv": "MN"
},
{
"usage": 893129,
"population": 4327541,
"value": 21,
"abv": "IL"
}
]
var k = [["abv", "population"]];
for(var i = 0; i < columns.length; i++){
k.push([columns[i].abv, columns[i].population]);
}
console.log(k) // Expected expected output
如果您需要,这里有一个通用的实现。
var names = ["abv", "population"];
var k = GetPropertiesInArray(columns, names); //[["abv", "population"]];
function GetPropertiesInArray(columns, names){
var m = [];
m.push(names);
for(var i = 0; i < columns.length; i++){
var item = [];
for(var j in names){
item.push(columns[i][names[j]]);
}
m.push(item);
}
return m;
}
console.log(k);//Expected output
我会用减速器来做:
const cols = [
{
"usage": 395226,
"population": 1925117,
"value": 21,
"abv": "MN"
},
{
"usage": 893129,
"population": 4327541,
"value": 21,
"abv": "IL"
}
]
cols.reduce(function(acc, current) {
acc.push([current.abv, current.population])
return acc;
}, ["abv", "population"]);
或者如果您不喜欢使用 push
:
cols.reduce(function(acc, current) {
acc = [
...acc,
[current.abv, current.population]
]
return acc;
}, ["abv", "population"]);
您的第一个数据在所需输出中似乎是静态的:
const columns = [
{
"usage": 395226,
"population": 1925117,
"value": 21,
"abv": "MN"
},
{
"usage": 893129,
"population": 4327541,
"value": 21,
"abv": "IL"
}
];
let cols = [];
if (columns.length) {
cols.push(["abv", "population"]);
columns.forEach(item => {
cols.push([item.abv, item.population])
});
}
console.log(cols);
我最终会使用这样的东西:
const columns = [
{
"usage": 395226,
"population": 1925117,
"value": 21,
"abv": "MN"
},
{
"usage": 893129,
"population": 4327541,
"value": 21,
"abv": "IL"
}
];
let converted = columns.map((c) => [c.abv, c.population]);
converted.unshift(["abv", "population"]);