TS
arrData = new BehaviorSubject<any>([]);
ngOnInit() {
const dataArr1 = [
{
id: '1',
name: 'Room1',
spinning: true
},
{
id: '2',
name: 'Room2',
spinning: true
},
{
id: '3',
name: 'Room3',
spinning: true
},
{
id: '4',
name: 'Room4',
spinning: true
}
];
this.arrData.next(dataArr1);
const url = { id: '2', name: 'Room2', link: '/api/conditions'}
const arr = new Array();
arr.push(url);
this.arrData.value.map((item: any) => {
return {
id: item.id,
name: item.name,
spinning: arr.findIndex(e => {
return e.id === item.id
}) === -1
}
});
console.log(this.arrData.value);
}
Here is a sample output: https://stackblitz.com/edit/angular-msovvq?file=src/app/app.component.ts
The aim here is to modify the value of spinning to false.
In the given example, there's a data object { id: 2, name: 'Room2' }
, which I then push to create an array.
I'm using findIndex where dataArr1 id === arr id, but it doesn't change the spinning value to false in dataArr1.
It remains the same.
[
{
id: '1',
name: 'Room1',
spinning: true
},
{
id: '2',
name: 'Room2',
spinning: true
},
{
id: '3',
name: 'Room3',
spinning: true
},
{
id: '4',
name: 'Room4',
spinning: true
}
];
It should look like this.
[
{
id: '1',
name: 'Room1',
spinning: true
},
{
id: '2',
name: 'Room2',
spinning: false
},
{
id: '3',
name: 'Room3',
spinning: true
},
{
id: '4',
name: 'Room4',
spinning: true
}
];
The Room2
spinning will be changed to false.