我需要根据api中的值对array(formarray)的值进行排序。 我需要从中订购爱好阵列,
"hobbies": [
"eat",
"sleep",
"drink"
]
收件人
"hobbies": [
"sleep",
"eat",
"drink"
]
应该按照爱好安排订购,
ArrangedHobbies = [ {'eat': 1, 'sleep': 0, 'drink': 2}]
我需要根据ArrangedHobbies的顺序来排列hobbies数组的值。
我曾尝试使用es map
arrangeHobbyValues() {
this.SignupForm.get('hobbies').map( x => { // tranform the value with map });
}
在code上可以进行堆叠闪电战
答案 0 :(得分:0)
嗯,我按照您尝试的顺序添加了,吃>睡眠>喝
this.SignupForm.get('hobbies').value.sort().reverse();
答案 1 :(得分:-1)
在模糊控制中调用arrageHobbyValues
<input type="text" class="form-control" (blur)="arrangeHobbyValues()"
[formControlName]="i">
您的rangingHobbyValues可以像
arrangeHobbyValues() {
(this.SignupForm.get('hobbies') as FormArray).controls.sort((a,b)=>{
let orderA:any=this.ArrangedHobbies[0][a.value] || 9999999;
let orderB:any=this.ArrangedHobbies[0][b.value] || 9999999;
return orderB-orderA
})
但是,奇怪的是您的“ ArrangedHobbies”是对象数组吗????
//if your arrangeHobies would be like
ArrangedHobbies = [ 'eat','sleep','drink']
//then
arrangeHobbyValues() {
(this.SignupForm.get('hobbies') as FormArray).controls.sort((a,b)=>{
let orderA:any=this.ArrangedHobbies.indexOf(a.value);
let orderB:any=this.ArrangedHobbies.indexOf(b.value);
if (orderA==-1)
orderA=99999;
if (orderB==-1)
orderB=99999;
return orderB-orderA
})