在javascript中循环遍历数组中的对象
Posted
技术标签:
【中文标题】在javascript中循环遍历数组中的对象【英文标题】:Looping over objects in an array in javascript 【发布时间】:2018-07-10 01:24:15 【问题描述】:我对编码还很陌生。我确实搜索了论坛,但找不到我要解决的问题的答案。我写了一个基本的节点应用程序。它将用于跟踪公立学校系统的课后健身计划的效果。每次访问该计划后,学生都会回答一系列问题。教师查看数据以评估该计划。数据存储在 MongoDB 集合中:
var visitSchema = mongoose.Schema(
userid: String,
date: String,
activitylist: String,
presession: String,
health: String,
bestpart: String,
worstpart: String,
postsession: String,
approved:
type: Boolean,
default: false
);
我正在使用 d3 尝试显示一些图表,以帮助教师进行分析。这将包括报告与数组中某些项目相关的指标。例如,学生参加了多少次列出的 4 项活动中的一项,看看是否没有被利用。另一个是会前、会后和健康项目,因为这些项目来自无线电输入,是四个选项之一。 我成功地将 mongoDB 数据拉入控制台。
这里有一些访问示例:
["_id":"5a56c70d4035a90aafbe99f2","date":"Wed Jan 10 2018 20:08:13 GMT-0600 (CST)","userid":"5a54270477a5d3ff63803879","activitylist":"swimming","presession":"ok","bestpart":"water","worstpart":"splashing","postsession":"good","__v":0,"approved":true,"_id":"5a66ab728f12a65cadefb117","date":"Mon Jan 22 2018 21:26:42 GMT-0600 (CST)","userid":"5a54270477a5d3ff63803879","activitylist":"yoga","presession":"good","health":"good","bestpart":"hi","worstpart":"there","postsession":"verygood","__v":0,"approved":true,"_id":"5a6d0b815e8791217f50948a","date":"Sat Jan 27 2018 17:30:09 GMT-0600 (CST)","userid":"5a67ee7d0f7d0a0b7fb1ae2f","presession":"good","health":"verygood","bestpart":"good","worstpart":"hag","postsession":"good","__v":0,"approved":false,"_id":"5a6d0b905e8791217f50948b","date":"Sat Jan 27 2018 17:30:24 GMT-0600 (CST)","userid":"5a67ee7d0f7d0a0b7fb1ae2f","presession":"great","health":"good","bestpart":"hi","worstpart":"hello","postsession":"verygood","__v":0,"approved":false,"_id":"5a6d0d80f6d66c225d103783","date":"Sat Jan 27 2018 17:38:40 GMT-0600 (CST)","userid":"5a67ee980f7d0a0b7fb1ae30","activitylist":"basketball","presession":"great","health":"verygood","bestpart":"hi","worstpart":"hello","postsession":"verygood","__v":0,"approved":false,"_id":"5a6fe540695e704dd125355a","date":"Mon Jan 29 2018 21:23:44 GMT-0600 (CST)","userid":"5a67ee980f7d0a0b7fb1ae30","activitylist":"basketball","presession":"great","health":"good","bestpart":"bas","worstpart":"nas","postsession":"verygood","__v":0,"approved":false]
我还成功地将数据呈现在 d3 图表中。这是我卡住的地方。现在,我正在使用以下代码解析数据并填充图表。下面的示例适用于活动列表项。它遍历数据并计算数据中每个活动的数量:
function constructData(visitData)
let data = [ label: "basketball", value: 0 , label: "swimming", value: 0 , label: "yoga", value: 0 ]
visitData.forEach(function(visit)
console.log(visit.activitylist)
for (var i = 0; i < data.length; i++)
console.log(data[i])
//if (visit.activitylist in data[i])
if (visit.activitylist == data[i].label) data[i].value++
)
//change(data);
console.log(data);
return data;
所以这适用于解析数据,但我必须对我想要报告的集合中的每个项目执行类似的操作。我正在寻找一个更全局的函数,我可以将我感兴趣的项目(活动列表、presession 等)传递到其中,或者只是迭代集合中的每个(或一组)项目。我一直在玩下面的这些代码,但还没有让它工作。该数组只是一个简单的数组,因此我可以进行测试。
var data = [
'pear': 1,
'apple': 2
,
'pear': 5,
'mango': 10
,
'pear': 3,
'apple': 5,
]
let final = []
data.forEach((visit) =>
for (var key in visit)
if (visit.hasOwnProperty(key))
//console.log(visit[key], key)
final.forEach((f) =>
console.log(f)
if (f[key])
f[key]++
return;
)
console.log(key)
let obj =
obj[key] = 0;
final.push(obj)
)
console.log(final)
因此,对于上述函数的任何建议,可以循环数据以检查数组中的每个项目或我指定的项目,我们将不胜感激。正如我所说,我对此很陌生,所以如果我的措辞不完全清楚,我深表歉意。提前致谢。
【问题讨论】:
【参考方案1】:看起来您在如何最好地迭代对象属性方面存在冲突。您有很多选择,如果您想使用循环,您可以执行类似 for-in 的操作:
var visitSchema =
userid: 'String1',
date: 'String2',
activitylist: 'String3',
presession: 'String4',
health: 'String5',
bestpart: 'String6',
worstpart: 'String7',
postsession: 'String8',
approved:
type: true,
default: false
for (var x in visitSchema)
console.log(x + ' - ' + visitSchema[x]);
虽然使用循环遍历对象和数组肯定没有任何问题错误,但处理此任务的更现代(和巧妙)的方法是将数组方法链接在一起(三大方法是 map、filter并减少)。关于 here 有一篇很棒的文章。
【讨论】:
【参考方案2】:您可以尝试这样来解析数据,而无需使用太多循环。
const temp = [];
for (const d in this.data)
if (this.data[d])
temp.push(
name: d,
value: this.data[d],
);
【讨论】:
以上是关于在javascript中循环遍历数组中的对象的主要内容,如果未能解决你的问题,请参考以下文章
如何循环遍历对象数组并根据 JavaScript 中的条件添加新的对象键?