TS
tempThermometer = new BehaviorSubject<any>([]);
ngOnInit() {
let tmp = [
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "1",
location: "room1",
sensor: "ROOM#40041150#1",
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "2",
location: "room1",
sensor: "ROOM#40041150#2",
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "3",
location: "room1",
sensor: "ROOM#40041150#3",
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "4",
location: "room1",
sensor: "ROOM#40041150#4",
temperature: 33.5
}
];
this.tempThermometer.next(tmp.map((obj: any) => {
return {
...obj,
spinning: true
};
}));
let url = [{ id: '2', sensor: 'ROOM#40041150#2', link: '/api/conditions' }]
const arr = this.tempThermometer.value.reduce((acc: any, element: any) => {
acc.push({
id: element.id,
sensor: element.sensor,
humidity: element.humidity,
temperature: element.temperature,
date: element.date,
spinning: url.findIndex((x: any) => x.sensor === element.sensor && element.spinning !== false) === -1
});
// acc.push(element)
return acc;
}, []);
console.log(arr);
вот вывод: https://stackblitz.com/edit/angular-ltt235?file=src / app / app.component.ts
То, что я пытаюсь сделать здесь, это изменить значение счетчика, когда вызывается API.
потому что на моей стороне он называется API, и он вызывает повтор около 5 раз.
например, это данные.
[{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "1",
location: "room1",
sensor: "ROOM#40041150#1",
spinning: true,
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "2",
location: "room1",
sensor: "ROOM#40041150#2",
spinning: true,
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "3",
location: "room1",
sensor: "ROOM#40041150#3",
spinning: true,
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "4",
location: "room1",
sensor: "ROOM#40041150#4",
spinning: true,
temperature: 33.5
}
]
затем, когда он вызвал первый API и FINI sh получение / выборка данных.
это вывод:
[{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "1",
location: "room1",
sensor: "ROOM#40041150#1",
spinning: false,
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "2",
location: "room1",
sensor: "ROOM#40041150#2",
spinning: true,
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "3",
location: "room1",
sensor: "ROOM#40041150#3",
spinning: true,
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "4",
location: "room1",
sensor: "ROOM#40041150#4",
spinning: true,
temperature: 33.5
}]
первый элемент или датчик, значение вращения которого изменится на false.
, затем второй вызывается из API и Fini sh получение / выборка данных.
вот проблема.
[{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "1",
location: "room1",
sensor: "ROOM#40041150#1",
spinning: true,
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "2",
location: "room1",
sensor: "ROOM#40041150#2",
spinning: false,
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "3",
location: "room1",
sensor: "ROOM#40041150#3",
spinning: true,
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "4",
location: "room1",
sensor: "ROOM#40041150#4",
spinning: true,
temperature: 33.5
}]
это только изменяет значение на втором элементе, который является первым элементом и вторым элементом, оба из них, их вращение будет изменено на false, а остальное должно быть истинным.
что должно быть так.
[{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "1",
location: "room1",
sensor: "ROOM#40041150#1",
spinning: false,
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "2",
location: "room1",
sensor: "ROOM#40041150#2",
spinning: true,
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "3",
location: "room1",
sensor: "ROOM#40041150#3",
spinning: true,
temperature: 33.5
},
{
date: "2020-04-11 17:59:45.0",
humidity: 32.8,
id: "4",
location: "room1",
sensor: "ROOM#40041150#4",
spinning: true,
temperature: 33.5
}]
потому что я сейчас делаю, звоню им один за другим.