javascript - formatting unstructured array
问题描述
I currently have a flat dataset that i'd like to format differently with header names. I've tried searching up examples for something similar but i've not been able to find anything. I only need the first 3 items in the first array before creating a second one with the second set of data. The data i have is:
[
['Peoples Revolutionary Party',
'52',
'#F2D7D5',
'Peoples Revolutionary Party',
'Farmer Labor Party',
'40',
'#1db309',
'Farmer Labor Party',
'Democratic Party',
'145',
'#1c7eff',
'Democratic Party',
'NEW',
'1',
'#ff0000',
'NEW',
'States Rights Party',
'5',
'#011d57',
'States Rights Party',
'The Party',
'58',
'#800020',
'The Party',
'California National Party',
'9',
'#d9a517',
'California National Party',
'Independent',
'7',
'grey',
'Independent',
'United Foxboro Party',
'2',
'#0722ed',
'United Foxboro Party',
'Republican Party',
'75',
'red',
'Republican Party',
'Gulf Coast Party',
'16',
'#b4b710',
'Gulf Coast Party',
'Libertarian Party',
'11',
'#ffd700',
'Libertarian Party',
'American Fascist Party',
'8',
'#000000',
'American Fascist Party',
'American Nationalist Party',
'2',
'#5c4033',
'American Nationalist Party']
]
I'd like to get the data like this:
[
[name: "Peoples Revolutionary Party", info: {seats: "52", color: "#F2D7D5"}],
[name: "Peoples Revolutionary Party", info: {seats: "52", color: "#F2D7D5"}],
[name: "Peoples Revolutionary Party", info: {seats: "52", color: "#F2D7D5"}],
[name: "Peoples Revolutionary Party", info: {seats: "52", color: "#F2D7D5"}],
[name: "Peoples Revolutionary Party", info: {seats: "52", color: "#F2D7D5"}],
[name: "Peoples Revolutionary Party", info: {seats: "52", color: "#F2D7D5"}],
[name: "Peoples Revolutionary Party", info: {seats: "52", color: "#F2D7D5"}],
[name: "Peoples Revolutionary Party", info: {seats: "52", color: "#F2D7D5"}],
[name: "Peoples Revolutionary Party", info: {seats: "52", color: "#F2D7D5"}]
]
解决方案
Like that ?
const input = ['Peoples Revolutionary Party',
'52',
'#F2D7D5',
'Peoples Revolutionary Party',
'Farmer Labor Party',
'40',
'#1db309',
'Farmer Labor Party',
'Democratic Party',
'145',
'#1c7eff',
'Democratic Party',
'NEW',
'1',
'#ff0000',
'NEW',
'States Rights Party',
'5',
'#011d57',
'States Rights Party',
'The Party',
'58',
'#800020',
'The Party',
'California National Party',
'9',
'#d9a517',
'California National Party',
'Independent',
'7',
'grey',
'Independent',
'United Foxboro Party',
'2',
'#0722ed',
'United Foxboro Party',
'Republican Party',
'75',
'red',
'Republican Party',
'Gulf Coast Party',
'16',
'#b4b710',
'Gulf Coast Party',
'Libertarian Party',
'11',
'#ffd700',
'Libertarian Party',
'American Fascist Party',
'8',
'#000000',
'American Fascist Party',
'American Nationalist Party',
'2',
'#5c4033',
'American Nationalist Party'];
let i,j,chunk = 4
const output = [];
for ( i=0, j=input.length; i<j; i+=chunk) {
let part = input.slice(i,i+chunk);
output.push({
name: part[0],
info: {
color: part[2],
seats: part[1]
}
});
}
console.log(output)
推荐阅读
- javascript - Google Analytics API 如何使用节点的 googleapis 包查询多个 URL?
- php - PHP:从字典列表中按键获取值列表
- ruby - Ruby DELETE 方法被识别为 GET
- javascript - 删除“
- " 使用 e.target.parentElement.remove()
- javascript - ReactJS Hooks 与全局变量
- assembly - 数据库是如何工作的?汇编器输出代码的字节不是吗?
- c# - 从对象列表中的项目填充 DataGridView,其中 2 列应该是第一列中不同对象的属性
- javascript - Gulp 没有开始?
- javascript - 尝试使用 Angular 实现“diskusage”包
- c# - 在 c# 中单击窗口窗体中的更新按钮时更新 Datagridview 数据