Home > Net >  How can I create new array with given list
How can I create new array with given list

Time:11-24

I have below array and list in JavaScript.

const headr = [H1, H2 ,H3, H4]
const Year = ['1Y', '2Y' ,'3Y', '4Y']
let arr_ = {
    H1 : Array(3)
    0 : {Year: '1Y', Type: 'TypeA', Value: -41445643364}
    1 : {Year: '2Y', Type: 'TypeA', Value: -11928080380}
    2 : {Year: '3Y', Type: 'TypeA', Value: 32370503415}
    H4 : Array(4)
    0 : {Year: '1Y', Type: 'TypeA', Value: -41445643364}
    1 : {Year: '2Y', Type: 'TypeB', Value: -11928080380}
    2 : {Year: '3Y', Type: 'TypeA', Value: 32370503415}
    3 : {Year: '4Y', Type: 'TypeA', Value: 32370503415}
}

and, I want to create new array as following format :

H1 : Array(3)
    0 : {Year: '1Y', Type: 'TypeA', Value: -41445643364}
    1 : {Year: '2Y', Type: 'TypeA', Value: -11928080380}
    2 : {Year: '3Y', Type: 'TypeA', Value: 32370503415}
    3 : {Year: '4Y', Type: '-', Value: 0}

    H2 : Array(4)
    0 : {Year: '1Y', Type: '-', Value: 0}
    1 : {Year: '2Y', Type: '-', Value: 0}
    2 : {Year: '3Y', Type: '-', Value: 0}
    3 : {Year: '4Y', Type: '-', Value: 0}

    H3 : Array(4)
    0 : {Year: '1Y', Type: '-', Value: 0}
    1 : {Year: '2Y', Type: '-', Value: 0}
    2 : {Year: '3Y', Type: '-', Value: 0}
    3 : {Year: '4Y', Type: '-', Value: 0}
    
    H4 : Array(4)
    0 : {Year: '1Y', Type: 'TypeA', Value: -41445643364}
    1 : {Year: '2Y', Type: 'TypeB', Value: -11928080380}
    2 : {Year: '3Y', Type: 'TypeA', Value: 32370503415}
    3 : {Year: '4Y', Type: 'TypeA', Value: 32370503415}
}

How can I achieve the array as the format? I've thinking creating new dictionary or array as below format and using for loop then push value.

let dict_ = {
        'H1' : [], 'H2' : [], 'H3' : [], 'H4' : []
    }

CodePudding user response:

You should use a map to store the data. The map will have the header as key and the array of data as value. You can use the map to get the data for each header.

const headr = ['H1', 'H2' ,'H3', 'H4']
const Year = ['1Y', '2Y' ,'3Y', '4Y']

let arr_ = {
    H1 : Array(3),
    0 : {Year: '1Y', Type: 'TypeA', Value: -41445643364},
    1 : {Year: '2Y', Type: 'TypeA', Value: -11928080380},
    2 : {Year: '3Y', Type: 'TypeA', Value: 32370503415},
    H4 : Array(4),
    0 : {Year: '1Y', Type: 'TypeA', Value: -41445643364},
    1 : {Year: '2Y', Type: 'TypeB', Value: -11928080380},
    2 : {Year: '3Y', Type: 'TypeA', Value: 32370503415},
    3 : {Year: '4Y', Type: 'TypeA', Value: 32370503415}
}

let map = new Map();

for (let i = 0; i < headr.length; i  ) {
    let header = headr[i];
    let data = arr_[header];
    if (data) {
        map.set(header, data);
    } else {
        let emptyData = [];
        for (let j = 0; j < Year.length; j  ) {
            emptyData.push({
                Year: Year[j],
                Type: '-',
                Value: 0
            });
        }
        map.set(header, emptyData);
    }
}

You don't have to copy paste the code, I just want to give you an idea of how I would solve this.

I'm also not entirely sure what you wanted but i think thats it.

If it helped, please give me feedback by upvoting / accepting my answer.

CodePudding user response:

const headers = ['H1', 'H2' ,'H3', 'H4'];
const years = ['1Y', '2Y' ,'3Y', '4Y'];
const obj = {
  H1: [
    {Year: '1Y', Type: 'TypeA', Value: -41445643364},
    {Year: '2Y', Type: 'TypeA', Value: -11928080380},
    {Year: '3Y', Type: 'TypeA', Value: 32370503415},
  ],
  H4: [
    {Year: '1Y', Type: 'TypeA', Value: -41445643364},
    {Year: '2Y', Type: 'TypeB', Value: -11928080380},
    {Year: '3Y', Type: 'TypeA', Value: 32370503415},
    {Year: '4Y', Type: 'TypeA', Value: 32370503415},
  ]
}

const result = headers.reduce((acc, h) => {
  acc[h] = [...obj[h] || []];
  years.forEach(y => {
    if (!acc[h].some(({Year}) => Year === y)) {
      acc[h].push({
        Year: y,
        Type: '-',
        Value: 0,
      });
    }   
  });
  return acc;
}, {})

console.log(result);

  • Related