Arrays 通过数组字段中的数组查找
如何通过一些也包含数组的数组查找内容? 更准确地说: 我想从Coach数组返回与用户名匹配的id(在Coach中)。我所尝试的:Arrays 通过数组字段中的数组查找,arrays,angular,Arrays,Angular,如何通过一些也包含数组的数组查找内容? 更准确地说: 我想从Coach数组返回与用户名匹配的id(在Coach中)。我所尝试的: if (!(args['coach'].value === '') && (args['coach'].value !== null)) { coachId = this.items.find(x => x.username === args.coach.value).id; } 基本上,this.items是我以前在cons
if (!(args['coach'].value === '') && (args['coach'].value !== null)) {
coachId = this.items.find(x => x.username === args.coach.value).id;
}
基本上,this.items
是我以前在console.log上看到的。现在它给了我未定义的
有人能解决这个问题吗?多谢各位
[
{
"id":584,
"name":"Name",
"coaches":[
{
"id":8587,
"username":"test"
},
{
"id":8589,
"username":"test1"
}
]
},
{
"id":587,
"name":"O1",
"coaches":[
]
}
]
假设我想在搜索名称时返回id
8587
,test
,Combinemap
和find
:
const array = [
[{a1: 1},
{a2: 2}
],
[{a1: 1},
{a2: 2},
{a3: 3}]
];
const element = array.map(innerArray => {
const found = innerArray.find(el => el.a1 === 1);
if (found) return found.a1;
return null;
}).find(el => el !== null);
console.log(element) // 1
组合
映射
和查找
:
const array = [
[{a1: 1},
{a2: 2}
],
[{a1: 1},
{a2: 2},
{a3: 3}]
];
const element = array.map(innerArray => {
const found = innerArray.find(el => el.a1 === 1);
if (found) return found.a1;
return null;
}).find(el => el !== null);
console.log(element) // 1
要查找多个匹配项,请执行以下操作:
const数据=[{
“id”:584,
“名称”:“名称”,
“教练”:[{
“id”:8587,
“用户名”:“测试”
},
{
“id”:8589,
“用户名”:“test1”
}
]
},
{
“id”:587,
“名称”:“O1”,
“教练”:[
]
}
];
const usernameToSearch='test1';
const foundCoachIds=数据
.减少((会计科目,当前)=>{
//首先分解Coach属性
常数{
教练,
休息
}=货币;
//检查是否有用户名与coach匹配
const foundMatches=coach.filter(x=>x.username===usernameToSearch);
//如果发现任何匹配项,则推入蓄能器
if(foundMatches.length){
for(const foundMatch of foundMatches){
if(acc.indexOf(foundMatch.id)=-1){
acc.push(foundMatch.id);
}
}
}
返回acc;
}, []);
控制台日志(foundCoachIds)查找多个匹配项的代码>,请执行以下操作:
const数据=[{
“id”:584,
“名称”:“名称”,
“教练”:[{
“id”:8587,
“用户名”:“测试”
},
{
“id”:8589,
“用户名”:“test1”
}
]
},
{
“id”:587,
“名称”:“O1”,
“教练”:[
]
}
];
const usernameToSearch='test1';
const foundCoachIds=数据
.减少((会计科目,当前)=>{
//首先分解Coach属性
常数{
教练,
休息
}=货币;
//检查是否有用户名与coach匹配
const foundMatches=coach.filter(x=>x.username===usernameToSearch);
//如果发现任何匹配项,则推入蓄能器
if(foundMatches.length){
for(const foundMatch of foundMatches){
if(acc.indexOf(foundMatch.id)=-1){
acc.push(foundMatch.id);
}
}
}
返回acc;
}, []);
控制台日志(foundCoachIds)代码>
用于外部id
data.map(it => {return !!it.coaches.find(it2 => it2.username == "test") ? it.id : null}).filter(it=>!!it)
evaluates to [584]
对于内部
(教练)身份证:
只需要从这些中选择第一项就可以得到答案
let y = this.items.filter(o => o.coaches.some(e => e.username === 'test'))[0]
.coaches.filter(e=>e.username === 'test')[0].id;
console.log(y);
用于外部id
data.map(it => {return !!it.coaches.find(it2 => it2.username == "test") ? it.id : null}).filter(it=>!!it)
evaluates to [584]
对于内部
(教练)身份证:
只需从中选取第一项即可获得答案。您可以粘贴此项吗。itemsJSON@Sajeetharan编辑了question.8587,例如,如果我使用名称test.data.map(it=>it.coach.find(it2=>it2.username==“test”)).filter(it=>!!it.map(it=>it.id)进行搜索谢谢你的回复。你能粘贴这个。项目吗JSON@Sajeetharan编辑了问题。8587例如,如果我使用名称test.data.map(it=>it.coach.find(it2=>it2.username==“test”))。filter(it=>!!it)。map(it=>it.id)感谢您的回答。很好的解决方案,您只需使用find
代替filter
,并删除|124;[]
,就像删除[0]
一样,这是否涉及多个命中?我想没有。不是最好的答案imo@Uzer你说的一次打击是什么意思?我没有得到?注册。这一行if(foundCoach[0]&&foundCoach[0].id)
,如果用户名
在coach
数组的第二个元素中找到怎么办?@Uzer修改了我的answerNice解决方案,你只需使用查找
来代替过滤器
,并删除|【/code>和[0]一样多的内容即可
这是否涉及多个点击?我想没有。不是最好的答案imo@Uzer你说的一次打击是什么意思?我没有得到?注册。此行if(foundCoach[0]&&foundCoach[0].id)
,如果在coach
数组的第二个元素中找到用户名
该怎么办?@Uzer修改了我的回答我收到了错误消息:innerArray.find不是函数
你必须为你的对象调整它。我收到了错误消息:innerArray.find不是函数
出于某种原因,你必须为你的对象调整它它给了我[数字];是的,Coach数组中的id。这是我在评论中的第一个答案。。。不管出于什么原因,它给了我[数字];是的,Coach数组中的id。这是我在评论中的第一个答案。。。没有关系
let y = this.items.filter(o => o.coaches.some(e => e.username === 'test'))[0]
.coaches.filter(e=>e.username === 'test')[0].id;
console.log(y);