创建包含从 parent 组件发送的 objects 值的单个数组
Create single Array that contains values of objects sent from parent component
您好,我需要在 angular 中创建单个数组,其中包含从 parent 组件
发送的 object 的值
objects 发送自 parent console.log('object',o):
object {type: "TRA", designator: "EPTR130B", availabilities: Array(1), info: Array(2), geometry: {…}}
object {type: "TRA", designator: "EPTR130A", availabilities: Array(1), info: Array(2), geometry: {…}}
object {type: "TRA", designator: "EPTR25", availabilities: Array(1), info: Array(2), geometry: {…}}
...etc
将其传递给 child 组件:
this.dataSend.emit(object.designator);
正在 child 中接收数据:
if (this.dataSend) {
this.dataSend
.subscribe(data => {
console.log(data);
});
}
console.log(data):
EPTR130B
EPTR130A
EPTR25
...etc
我应该怎么做才能接收像 [EPTR130B、EPTR130A、EPTR25...] 这样的数组?地图?推?
编辑:
我做了一些更改并将数据作为 object 传递给 child 和 this.dataSend.emit({data});
并收到:
{data: Array(63)}
data: Array(63)
0: {type: "ADHOC", designator: "UAV1", availabilities: Array(1), info: Array(2), geometry: {…}}
1: {type: "TRA", designator: "EPTR130B", availabilities: Array(1), info: Array(2), geometry: {…}}
2: {type: "TRA", designator: "EPTR130A", availabilities: Array(1), info: Array(2), geometry: {…}} ...etc
你应该可以使用 Rxjs 的 map
和数组的 map
来完成。
尝试:
import { filter, map, tap } from 'rxjs/operators'
......
this.dataSend
.pipe(
tap((dataArray: any) => console.log(`dataArray before filter: ${dataArray}`)),
// allow emissions where dataArray is truthy and it has length > 0
filter((dataArray: any) => !!dataArray && dataArray.length),
tap((dataArray: any) => console.log(`dataArray after filter: ${dataArray}`)),
map((dataArray: any) => dataArray.map(data => data.designation)),
)
.subscribe(data => {
console.log(data);
});
=======================编辑===================== =========
使用 tap
运算符进行调试,您可以揭开帷幕并使用可观察对象及时查看发生了什么。
我一直在尝试一些事情,我不知道它是否是对数组和对象进行操作的简洁方式,但它确实有效:
.subscribe(data => {
console.log(data);
let arr: any[] = [];
Object.keys(data).forEach(function(item) {
for(let i=0; i<data[item].length; i++){
arr.push(data[item][i].designator);
}
console.log(arr);
});
});
您好,我需要在 angular 中创建单个数组,其中包含从 parent 组件
发送的 object 的值objects 发送自 parent console.log('object',o):
object {type: "TRA", designator: "EPTR130B", availabilities: Array(1), info: Array(2), geometry: {…}}
object {type: "TRA", designator: "EPTR130A", availabilities: Array(1), info: Array(2), geometry: {…}}
object {type: "TRA", designator: "EPTR25", availabilities: Array(1), info: Array(2), geometry: {…}}
...etc
将其传递给 child 组件:
this.dataSend.emit(object.designator);
正在 child 中接收数据:
if (this.dataSend) {
this.dataSend
.subscribe(data => {
console.log(data);
});
}
console.log(data):
EPTR130B
EPTR130A
EPTR25
...etc
我应该怎么做才能接收像 [EPTR130B、EPTR130A、EPTR25...] 这样的数组?地图?推?
编辑:
我做了一些更改并将数据作为 object 传递给 child 和 this.dataSend.emit({data});
并收到:
{data: Array(63)}
data: Array(63)
0: {type: "ADHOC", designator: "UAV1", availabilities: Array(1), info: Array(2), geometry: {…}}
1: {type: "TRA", designator: "EPTR130B", availabilities: Array(1), info: Array(2), geometry: {…}}
2: {type: "TRA", designator: "EPTR130A", availabilities: Array(1), info: Array(2), geometry: {…}} ...etc
你应该可以使用 Rxjs 的 map
和数组的 map
来完成。
尝试:
import { filter, map, tap } from 'rxjs/operators'
......
this.dataSend
.pipe(
tap((dataArray: any) => console.log(`dataArray before filter: ${dataArray}`)),
// allow emissions where dataArray is truthy and it has length > 0
filter((dataArray: any) => !!dataArray && dataArray.length),
tap((dataArray: any) => console.log(`dataArray after filter: ${dataArray}`)),
map((dataArray: any) => dataArray.map(data => data.designation)),
)
.subscribe(data => {
console.log(data);
});
=======================编辑===================== =========
使用 tap
运算符进行调试,您可以揭开帷幕并使用可观察对象及时查看发生了什么。
我一直在尝试一些事情,我不知道它是否是对数组和对象进行操作的简洁方式,但它确实有效:
.subscribe(data => {
console.log(data);
let arr: any[] = [];
Object.keys(data).forEach(function(item) {
for(let i=0; i<data[item].length; i++){
arr.push(data[item][i].designator);
}
console.log(arr);
});
});