0

I have an array of exam result, it contains the date the exam was sat and the grade achieved. I want to put all the grades in one array and the dates in another so I can plot them in a chart.js application. How can I separte them?

Results Array example layout:

Results{

[0] {examDate: "2017-10-16T10:30:00", <ResultEntry>grade: A}

[1] {examDate: "2017-15-16T11:00:00", <ResultEntry>grade: C}

[2] {examDate: "2017-16-16T11:30:00", <ResultEntry>grade: B}

[3]{examDate: "2017-20-16T12:00:00", <ResultEntry>grade: B}

}

But what I try doesn't populate the two new arrays

var dateArray;
var gradeArray;
var counter = 0;

for (members in results) {
    dateArray[counter] = members[0];
    gradeArray[counter] = members[1];
    counter++;//increment storage index
}

I have Jquery installed on my project can the .each functionality be used to achieve this?

1
  • Try dateArray[counter] = members.examDate and dateArray[counter] = members.grade Commented Oct 24, 2017 at 9:03

4 Answers 4

3

You can use map method by passing a callback function.

The map() method creates a new array with the results of calling a provided function(callback) on every element in the calling array.

let results=[{examDate: "2017-10-16T10:30:00", grade: 'A'},{examDate: "2017-15-16T11:00:00", grade: 'C'},{examDate: "2017-16-16T11:30:00", grade: 'B'},{examDate: "2017-20-16T12:00:00", grade: 'B'}];
let dates=results.map(function(a){ 
    return a.examDate;
});
let grades=results.map(function(a){
    return a.grade;
});
console.log(dates);
console.log(grades);

You can use also arrow functions.

 let grades=results.map(a => a.grade);
Sign up to request clarification or add additional context in comments.

Comments

0

You need to get values of fields from array elements. Check this:

var dateArray = [];
var gradeArray = [];
var counter = 0;

for (var member in results) {
    dateArray.push(member.examDate);
    gradeArray.push(member.grade);
}

Comments

0

If results is an array, then in this line

for (members in results) {

members is basically the index value 0,1,..

so members[0] will be undefined

I have Jquery installed on my project can the .each functionality be used to achieve this?

Yes, but why not use the native forEach instead

var dateArray = [], gradeArray = [];
results.forEach( function( result ){
  dateArray.push( result.examDate );
  gradeArray.push( result.grade );
});

Comments

0

Considering that the results array is has the following shape:

let results = [
    { examDate: "2017-10-16T10:30:00", grade: 'A' },
    { examDate: "2017-15-16T11:00:00", grade: 'C' },
    { examDate: "2017-16-16T11:30:00", grade: 'B' },
    { examDate: "2017-20-16T12:00:00", grade: 'B' }
];

Array.prototype.map is perfect for this purpose:

let dateArray = results.map(result => result.examDate);
let gradeArray = results.map(result => result.grade);

You can also loop over the elements in a few different ways:

let dateArray = [],
    gradeArray = [];

for (let result of results) {
    dateArray.push(result.examDate);
    gradeArray.push(result.grade);
}

And a solution similar to yours:

var dateArray = [],
    gradeArray = [];

for (var i = 0; i < results.length; i++) {
    dateArray[i] = results[i].examDate;
    gradeArray[i] = results[i].grade;
}

You can also push them into the array instead of setting a position to the value:

var dateArray = [],
    gradeArray = [];

for (var i = 0; i < results.length; i++) {
    dateArray.push(results[i].examDate);
    gradeArray.push(results[i].grade);
}

And using jQuery each as you mention:

var dateArray = [],
    gradeArray = [];

$.each(results , function(i, value) {
    dateArray.push(value.examDate);
    gradeArray.push(value.grade);
});

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.