【问题标题】:How to sum value in array im JavaScript?如何对数组 im JavaScript 中的值求和?
【发布时间】:2022-11-18 04:59:07
【问题描述】:

我有两个数组对象,我把这两个数组放在 Vue JS 3 中制作图表

数组 1 用于标题:

[
    "Jumlah SD",
    "Jumlah SMP",
    "Jumlah SD",
    "Jumlah SMP"
]

第二个数组是值:

[
    "22",
    "243",
    "44",
    "22"
]

我的问题是,如何对第二个数组求和?我预期的数组对象是: 标题的第一个数组:

[
    "Jumlah SD",
    "Jumlah SMP",
]

值的第二个数组将是:

[
    "66",
    "265",
]

我当前的代码是:

        onMounted(() => {
            chart.totalField = props.datas.length === 0 ? 0 : JSON.parse(props.datas[0].fieldDatas).length
            chart.totalData = props.datas.length

            if (chart.total !== 0) {
                for (let i = 0; i < chart.totalData; i++) {
                    for (let j = 0; j < chart.totalField; j++) {
                        chart.title.push(JSON.parse(props.datas[i].fieldDatas)[j].title)
                        chart.value.push(JSON.parse(props.datas[i].fieldDatas)[j].value)
                    }
                }
            }
            console.log(chart.title);
            console.log(chart.value);
        })

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    您可以使用reduce方法,将arr1中的项目分组,总和为arr2

    const arr1 = [
        "Jumlah SD",
        "Jumlah SMP",
        "Jumlah SD",
        "Jumlah SMP"
    ]
    
    const arr2 = [
        "22",
        "243",
        "44",
        "22"
    ]
    
    const result = arr1.reduce((acc, item, index) => {
      let value = acc[item];
      let count = +arr2[index]; 
      return {
        ...acc,
        [item]: value ? value += count : count
      }
    }, {})
    
    console.log(result)
    console.log(Object.values(result))
    console.log(Object.keys(result))

    【讨论】:

    • 纳赛尔的回答很棒
    【解决方案2】:

    我会将数组中的数据数据按摩到一个对象中。我们将遍历数组,一次一步构建对象。下面是我们想要实现的伪代码/计划:

      let obj = { };
      obj = { "Jumlah SD": 22 }; // i = 0
      obj = { "Jumlah SD": 22, "Jumlah SMP": 243}; // i = 1
      obj = { "Jumlah SD": 66, "Jumlah SMP": 243}; // i = 2
      obj = { "Jumlah SD": 66, "Jumlah SMP": 265}; // i = 3
      Object.keys(obj); // [ "Jumlah SD", "Jumlah SMP" ]
      Object.values(obj); // [ 66, 265 ]
    

    下面是实现上述内容的 Javascript:

    const keys = [
        "Jumlah SD",
        "Jumlah SMP",
        "Jumlah SD",
        "Jumlah SMP"
    ];
    const values = [
        "22",
        "243",
        "44",
        "22"
    ];
    let obj = { };
    for (let i = 0; i < keys.length && i < values.length; i++) {
        let key = keys[i];
        if (!obj[key]) obj[key] = 0;
        obj[key] += Number(values[i]);
        console.log("i:",i,"obj:", JSON.stringify(obj));
    }
    console.log("obj:",JSON.stringify(Object.keys(obj)));
    console.log("values:",JSON.stringify(Object.values(obj))); // numeric version of the values
    console.log("values:",JSON.stringify(Object.values(obj).map( n => n.toString() ))); // string version of the values

    【讨论】:

      【解决方案3】:

      尝试这个:

      const arr1 = [
          "Jumlah SD",
          "Jumlah SMP",
          "Jumlah SD",
          "Jumlah SMP"
      ];
      
      const arr2 = [
          "22",
          "243",
          "44",
          "22"
      ];
      
      dict = {}
      
      for (let i = 0; i < arr1.length; i++) {
          const key = arr1[i];
          if (key in dict) {
              dict[key] = String(Number(dict[key]) + Number(arr2[i]));
          } else {
              dict[key] = String(Number(arr2[i]));
          }
      }
      
      const arr3 = Object.keys(dict);
      const arr4 = Object.values(dict);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-20
        • 2013-04-10
        • 2018-06-22
        • 1970-01-01
        • 1970-01-01
        • 2020-01-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多