'Pull same key out of multiple objects

I have JSON data like this

{
"_id": "621813467b8cfa82889de451",
"name": "Test Student",
"parentEmail": "[email protected]",
"teacher": "Mr Teacher",
"scores": [
    {
    "Raise Hand": "0",
    "Ask For Help": "2",
    "email": "[email protected]",
    "date": "2022-02-24"
    },
    {
    "Raise Hand": "2",
    "Ask For Help": "4",
    "email": "[email protected]",
    "date": "2022-02-24"
    }
],
"goals": [
    {
    "type": "Multi Choice",
    "name": "Raise Hand",
    "_id": "6218140f7b8cfa82889de45f"
    },
    {
    "type": "Multi Choice",
    "name": "Ask For Help",
    "_id": "621814187b8cfa82889de470"
    }
],
"__v": 4
}

and I am trying to get the "scores" data to have a bar graph with chart.js (Use date as x axis and the scores for the y axis), but I don't know how to extract the data dynamically.

I need it to be dynamic so a user can add a new goal through the GUI and submit a score for a new day and either a new graph is added or a new bar is added to the bar graph.

I'm also using svelte.

This is what I have so far:

//dummy data to resemble data from database
const students = [
    {
    email: "ads",
        "Raise Hand": 1,
        "Ask For Help": 2,
        date: "2022-2-23",
        comment: "Good",
    },
    {
    email: "ads",
        "Raise Hand": 3,
        "Ask For Help": 4,
        date: "2022-2-24",
        comment: "No Comment",
    },
    {
    email: "ads",
        "Raise Hand": 1,
        "Ask For Help": 2,
        date: "2022-2-25",
        comment: "No Comment",
    },
    {
    email: "ads",
        "Raise Hand": 3,
        "Ask For Help": 4,
        "Do Work": 3,
        date: "2022-2-25",
        comment: "No Comment",
    },
    {
    email: "ads",
        "Raise Hand": 1,
        "Ask For Help": 2,
        "Do Work": 3,
        "New One": 4,
        date: "2022-2-25",
        comment: "No Comment",
    },
];

let data = [];

let newKey;

getKeys();

async function getKeys() {
    for (let i = 0; i < students.length; i++) {
        for (let key in students[i]) {
            if (
        key == "email" ||
                key == "date" ||
                key.toLowerCase().includes("comment")
            ) {
                continue;
            } else {
                newKey = key;
                const newValue = students[i][key];
                const amountOfKeys = Object.keys(students[i]).length;

                await checkKey(key, i)
            }
        }
    }
  filter(data)
}

function checkKey(key, num) {
  let newArr = [];
  let tempObj = {}
    if (key == newKey) {
    tempObj[key] = students[num][key]
    tempObj["date"] = students[num]["date"]
        data.push(tempObj);
    // console.log(data)
    }
}

function filter(arr) {
  console.log(arr)
  let temp;
  for(let i = 0; i < arr.length; i++) {
    temp = arr[i]
    arr.splice(0, 1)
    console.log(temp)
    console.log(arr)
  }
}

Edit: Not sure if it matters but on the backend I'm using NodeJs with express and mongoDB



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source