1

I have two array object, and i put that two array to make a chart in Vue JS 3

array 1 is for the title:

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

and second array is the value:

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

My question is, how to sum the second array? My expected array object is: first array for title:

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

and second array for value will be:

[
    "66",
    "265",
]

My current code is:

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

3 Answers 3

4

You can use reduce method, to group the by the items in the arr1 with sum of 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))

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

1 Comment

Nasser's answer is great
0

I would data-massage the data from the arrays into an object. We would iterate through the arrays building up the object one step at a time. Below is the pseudo-code / plan of what we want to achieve:

  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 ]

Below is Javascript that implements the above:

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

Comments

0

Try this:

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

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.