Home > Software engineering >  Converting 3 arrays into an object
Converting 3 arrays into an object

Time:04-23

Good day buddy, i'm trying to convert 3 arrays into 1 object. I need each months array length to be in accordance with another arrays length If some one has any idea how to solve it i would be appreciated too much. Thanks for your time and reading this, have a nice day!

Data:

const months = [
    "March 2022",
    "April 2022",
    "May 2022",
    "June 2022"
]

const values = [
    [ "-50" ],
    [ "-100",  "350", "-111" ],
    [ "201", "200" ],
    [ "-290" ]
]

const categories = [
    [ "Credit" ],
    [ "Debt", "Salary", "Credit" ],
    [ "Salary", "Salary" ],
    [ "Investments" ]
]

This is expected result:

[
    {
        "name": "March 2022",
        "series": 
            { 
                "name": "Credit" , 
                "value": -50 
            }
        
    },
    {
        "name": "April 2022",
        "series": [
            {
                "name": "Debt",
                "value": -100
            },
            {
                "name": "Salary",
                "value": 350
            },
            {
                "name": "Credit",
                "value": -111
            }
        ]
    },
    {
        "name": "May 2022",
        "series": [
            {
                "name": "Salary",
                "value": 201
            },
            {
                "name": "Salary",
                "value": 200
            }
        ]
    },
    {
        "name": "June 2022",
        "series":
            {
                "name": "Investments",
                "value": -290
            }
        
    }
]

All what i did in last days is this code, but it's not working, before for length of second elements it was working, but, when length of 3rd element increased they start staking together, and this is not all.. I'm using angular's service, and when navigating from 1 route to another their length by index[i] start staking (if manually refresh page by crtl r it's working).. When firts call in first for loop of console.log(values[i].length) its length are [1, 3, 2, 1] which is correct, but at next call(while going thougth pages) length is [2, 6, 4, 2] and increasing with each call:

const months = [
  "March 2022",
  "April 2022",
  "May 2022",
  "June 2022"
]

const values = [
  ["-50"],
  ["-100", "350", "-111"],
  ["201", "200"],
  ["-290"]
]

const categories = [
  ["Credit"],
  ["Debt", "Salary", "Credit"],
  ["Salary", "Salary"],
  ["Investments"]
]

let result = [];
let subArray1 = [];
let subArray2 = [];

for (let i = 0; i < values.length; i  ) {

  // if 1 expense per month push to subArray1 => result
  if (values[i].length == 1) {
    subArray1.push([{
      "name": categories[i],
      "value":  values[i]
    }])
  } else {

    for (let j = 0; j < values[i].length; j  ) {
      // if more then 1 expense per month push to subArray2 => subArray1 => result
      subArray2.push({
        "name": categories[i][j],
        "value":  values[i][j]
      });
    };
    subArray1.push(subArray2);
  };
};


//combine all together
for (let i = 0; i < months.length; i  ) {
  result.push({
    "name": months[i],
    "series": subArray1[i]
  });
};

CodePudding user response:

To achieve your result, I have created a function which constructs the series array and a main function which uses the series function into it.

let months = [
  "March 2022",
  "April 2022",
  "May 2022",
  "June 2022"
]

let values = [
  ["-50"],
  ["-100", "350", "-111"],
  ["201", "200"],
  ["-290"]
]

const categories = [
  ["Credit"],
  ["Debt", "Salary", "Credit"],
  ["Salary", "Salary"],
  ["Investments"]
]

const mapSeries = (index) => (values[index] || []).map((value, vindex) => ({
  name: categories[index][vindex],
  value
}))

const output = months.map((name, index) => ({
  name,
  series: mapSeries(index)
}))

console.log(output)

Note: If each element array length of values and categories are not same, then in the mapSeries function, we can do something like categories[index]?.[vindex] || 'not assigned') to avoid errors.

CodePudding user response:

You're almost there you just need a small change in your code and you'll be good.

  1. You need to initialise subArray2 inside loop on each iteration as we need separate array every time.
  2. You can drop off if condition as the inner for loop will anyway takes care of it irrespective of number of values

let months = ["March 2022","April 2022","May 2022","June 2022"]
let values = [["-50"],["-100", "350", "-111"],["201", "200"],["-290"]]
const categories = [["Credit"],["Debt", "Salary", "Credit"],["Salary", "Salary"],["Investments"]]

let result = [];
let subArray1 = [];

for (let i = 0; i < values.length; i  ) {
    let subArray2 = [];
    for (let j = 0; j < values[i].length; j  ) {
      subArray2.push({
        "name": categories[i][j],
        "value":  values[i][j]
    });
  };
  subArray1.push(subArray2)
};

//combine all together
for (let i = 0; i < months.length; i  ) {
  result.push({
    "name": months[i],
    "series": subArray1[i]
  });
};

console.log(result)

CodePudding user response:

You can generate the results you want with a nested map, first on the month name and then the categories, using the second (index) parameter to the callback to select the appropriate values from the other arrays:

const months = [
  "March 2022",
  "April 2022",
  "May 2022",
  "June 2022"
]

const values = [
  ["-50"],
  ["-100", "350", "-111"],
  ["201", "200"],
  ["-290"]
]

const categories = [
  ["Credit"],
  ["Debt", "Salary", "Credit"],
  ["Salary", "Salary"],
  ["Investments"]
]

const result = months.map((m, mnum) => ({
  name: m,
  series: categories[mnum].map((c, cnum) => ({
    name: c,
    value:  values[mnum][cnum]
  }))
}))

console.log(result)

Note that this code always returns an array for series, it seems that would be easier to process than trying to figure out if was an array or an object.

  • Related