如何使用javascript在forEach循环中过滤结果

如何使用javascript在forEach循环中过滤结果,javascript,arrays,foreach,Javascript,Arrays,Foreach,我很难弄清楚如何在数组上迭代,并且只有在找到特定值时才执行某些操作 非常感谢您的帮助 我的想法是: ForEach Entry, Where X = Y { console.log('I did something'); } 实际数据: [{ "id" : 0, "fullName" : "George", "email": "george@test.ca", "group": 'Faculty', "totalFiles": 12, "out

我很难弄清楚如何在数组上迭代,并且只有在找到特定值时才执行某些操作

非常感谢您的帮助

我的想法是:

ForEach Entry, Where X = Y {
 console.log('I did something');
}
实际数据:

[{
    "id" : 0,
    "fullName" : "George",
    "email": "george@test.ca",
    "group": 'Faculty',
    "totalFiles": 12,
    "outstandingFiles": 10,

},
{
    "id" : 1,
    "fullName" : "Albert",
    "email": "albert@test.ca",
    "group": 'Student',
    "totalFiles": 15,
    "outstandingFiles": 8,
}];

有很多选项可以在没有foreach的情况下过滤值,您可以使用find返回第一个匹配值

var myArray=[{
“id”:0,
“全名”:“乔治”,
“电子邮件”:george@test.ca",
“团体”:“教员”,
“totalFiles”:12,
“未完成文件”:10,
},
{
“id”:1,
“全名”:“阿尔伯特”,
“电子邮件”:albert@test.ca",
“团体”:“学生”,
“totalFiles”:15,
“未完成文件”:8,
}];
var result=myArray.find(t=>t.group=='Faculty');
控制台日志(结果)如果需要多个结果,或者希望查询的第一个结果,则可以使用

这是一个使用过滤器的示例

const data = [
    {
        "id" : 0,
        "fullName" : "George",
        "email": "george@test.ca",
        "group": 'Faculty',
        "totalFiles": 12,
        "outstandingFiles": 10,
    },{
        "id" : 1,
        "fullName" : "Albert",
        "email": "albert@test.ca",
        "group": 'Student',
        "totalFiles": 15,
        "outstandingFiles": 8,
    }
];

const result = data.filter(info => {
    return info.group === 'Faculty'
})
console.log(结果)
将输出

[ { id: 0,
    fullName: 'George',
    email: 'george@test.ca',
    group: 'Faculty',
    totalFiles: 12,
    outstandingFiles: 10 } ]

你可以在

你想过滤什么?我想过滤where Group='Faculty',例如
arr.filter(o=>o.Group=='Faculty”).forEach(o=>console.log(o))
中了解这个和更多的数组方法,可能是因为这个问题已经被问了无数次了,并且回答了无数次。抱歉,可能是重复的,我想我还不清楚,我需要在事后对结果做一个预测。首先使用这个过滤器,然后使用新的结果变量执行forEach?答案有用吗