0

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' : []
    }
1
  • I am confused about the arr_ variable. Could you format this and expected output properly? Commented Nov 24, 2022 at 0:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.