javascript - 比较两个数组并将结果推送到新数组
问题描述
我有两个完全用字符串填充的数组。我正在尝试比较字符串中的值,然后将字符串推送到一个空数组。
goupId = [
{'1','2','3','4'}]
homeGroups =
[{'2','3', '4','1'}]
sameId =[];
这是我的逻辑
compare: function(groupId, homeGroups) {
this.groupId.forEach((e1)=>this.homeGroups.foreach((e2)=>{
if(e1 === e2){
this.sameId.push(e1)
}
}
));
}
我收到错误 TypeError: Cannot read property 'forEach' of undefined"
解决方案
您需要this.
在引用时删除它们groupID
,homeGroups
因为它们不是成员变量。this
您可以在此处了解更多信息。
您的代码最终将如下所示。
compare: function(groupId, homeGroups) {
groupId.forEach((e1)=>homeGroups.foreach((e2)=>{
if(e1 === e2){
this.sameId.push(e1)
}
}
));
}
推荐阅读
- amazon-s3 - boto3 S3Transfer 抛出错误 AttributeError: 'ResourceMeta' 对象没有属性 'events'
- ios - SwiftUI @State 更改正在显示的图像
- python - Python WAND 调整质量差
- python - 在 setuptools 的 extras_require 中从私有 PyPi 安装库
- python - Python中某些类型的数组
- javascript - 如何在 React.js 中加粗部分文本?
- google-sheets - 查询公式给出无法使用 =sum() 求和的值
- excel - 固定范围选择
- python - rest_framework 路由器的模板中的链接
- single-sign-on - Zendesk:管理员用户无法使用企业 SSO