Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
-1
rated 0 times [  5] [ 6]  / answers: 1 / hits: 42831  / 11 Years ago, wed, august 28, 2013, 12:00:00

I'm new to nodejs and mongodb. My problem is I've a json of following type



{ 
_id: 199,
name: 'Rae Kohout',
scores: [
{ type: 'exam', score: 82.11742562118049 },
{ type: 'quiz', score: 49.61295450928224 },
{ type: 'homework', score: 28.86823689842918 },
{ type: 'homework', score: 5.861613903793295 }
]
}


Here I want to compare score for the type 'homework' and remove the homework which has lowest score.To solve this I've written some code like



var low = '';
for(var i=0;i<doc.scores.length;i++)
{
if(doc.scores[i].type == 'homework'){
if(low == ''){
low = doc.scores[i].score;
}
if( doc.scores[i].score > low ){
console.log(index for lowest score is + i);
low = '';
}
}
}


Now I'm able to find the index for the lowest score, and want to removes values at that index. I tried to use Array.splice() method but that works on Array only. can anyone help me to solve it ?


More From » arrays

 Answers
5

Use splice like so:



doc.scores.splice(index, 1);

[#76058] Wednesday, August 28, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marinal

Total Points: 655
Total Questions: 99
Total Answers: 99

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
;