如何从JSON数组中删除元素 [英] How to remove element from JSON array

查看:529
本文介绍了如何从JSON数组中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的NodeJS和MongoDB。我的问题是我有以下类型的JSON

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 }
 ]
}

在这里,我想比较比分为类型'功课',并删除它有最低的功课score.To解决这个我已经写了一些code像

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 = '';
   }
 }
}

现在我能找到得分最低的指标,并希望该指数以消除值。我试图用方法Array.splice()方法,但适用于仅阵列。谁能帮助我解决这个问题?

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 ?

推荐答案

使用拼接像这样:

doc.scores.splice(index, 1);

这篇关于如何从JSON数组中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆