首页 > 解决方案 > 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"}]
] 

标签: javascriptarraysobject

解决方案


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)


推荐阅读