将 3 个数组转换为一个对象

Converting 3 arrays into an object

你好,伙计,我正在尝试将 3 个数组转换为 1 个对象。 我需要每个月的数组长度与另一个数组的长度一致 如果有人知道如何解决它,我将不胜感激。感谢您抽空阅读本文,祝您有愉快的一天!

数据:

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" ]
]

这是预期结果:

[
    {
        "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
            }
        
    }
]

我最近几天所做的就是这段代码,但它不起作用,在第二个元素的长度之前它是有效的,但是,当第三个元素的长度增加时,它们开始放在一起,这还不是全部。 . 我正在使用 angular 的服务,当从 1 条路线导航到另一条路线时,它们的长度按索引 [i] 开始放样(如果通过 crtl+r 手动刷新页面它正在工作)。当第一次打电话时对于 console.log(values[i].length) 的循环,它的长度是 [1, 3, 2, 1] 这是正确的,但是在下一次调用时(在浏览页面时)长度是 [2, 6, 4 , 2] 并随着每次调用而增加:

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

为了实现您的结果,我创建了一个构造 series 数组的函数和一个使用 series 函数的主函数。

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)

注: 如果 valuescategories 的每个元素数组长度不相同,那么在 mapSeries 函数中,我们可以做类似 categories[index]?.[vindex] || 'not assigned') 的操作来避免错误。

你就快完成了,你只需要对你的代码做一点小改动就可以了。

  1. 您需要在每次迭代时在循环内初始化 subArray2,因为我们每次都需要单独的数组。
  2. 你可以放弃 if 条件,因为无论值的数量如何,内部 for 循环都会处理它

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)

您可以使用嵌套的 map 生成您想要的结果,首先是 month 名称,然后是 categories,使用第二个 (index) 参数回调到 select 来自其他数组的适当值:

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)

请注意,此代码总是 returns series 的数组,这似乎比试图弄清楚是数组还是对象更容易处理。