Need help with this code logic. I am working on an array and function :
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name: any = 'World';
myArray: any =
[
{
id: 2001,
name:"a"
},
{
id: 2002,
name:""
},
{
id: 2003,
name:"b"
},
{
id: 2004,
name:"c"
},
];
ngOnInit() {
this.removeEmptyContent();
}
selectFile(event) {
console.log('event', event);
this.myArray.forEach( (myObject) => {
console.log('myObject', myObject);
if(myObject.length > -1){
console.log('completed');
} else {
console.log('in progress');
}
});
console.log('getlast', this.myArray.length > -1);
}
removeEmptyContent() {
this.myArray.forEach( (myObject, index) => {
if(!myObject.name){
this.myArray.splice(index, 1);
}
});
}
}
<hello name="{{ name }}"></hello>
<ul>
<li *ngFor="let item of myArray">
<div (click)=selectFile(item)>
{{item.name}}
</div>
</li>
</ul>
I need to change the value in the array when selecting the last item. When I select indexes 0, 1,.. the status should be "progress", but when I select the last index I want the status to be "complete". How can I achieve that?
Note: The code above requires fixing and updating
Thanks in advance