About Array js

let columns = ["name", "age", "weight"] // 
let  data = [["jack", 18, 50],["jenny", 22, 60]] 
let  arr = []

//

[
 {"name": "jack","age": 18,"weight": 50},{"name": "jenny","age": 22,"weight": 60},{"name": "","age": 40,"weight": 110}
]

{"name": "Total", "age": 40, "weight": 110} the last one is the cumulative addition of age and weight to the array push

Mar.23,2021

let all = {
  name: "",
  age: 0,
  weight: 0
}

data.forEach(item => {
  arr.push(
    {
      name: item[0],
      age: item[1],
      weight: item[2]
    };
  all.age = all.age + item[1];
  all.weight = all.weight + item[2];
});
arr.push(all);

        //reduce
        const initValue = {}
        columns.forEach((param, index) => initValue[param] = index == 0 ? '' : 0);

        data.reduce((total, current, index) => {
            //
            const item = {}
            columns.forEach((param, index) => {
                item[param] = current[index]
                //name,
                if (index != 0) total[param] += current[index]
            })
            //current pusharr
            arr.push(item)

            //,total pusharr
            if (index == data.length - 1) arr.push(total)
            return total
        }, initValue)

const summary = data.reduce((carry, item) => {
  arr.push({
    [columns[0]]: item[0],
    [columns[1]]: item[1],
    [columns[2]]: item[2],
  });

  carry[1] += item[1];
  carry[2] += item[2];
}, ['', 0 , 0]);

arr.push(summary);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b42133-2c5d7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b42133-2c5d7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?