javascript subsxribe中的Map导致未定义的错误

7hiiyaii  于 2023-03-28  发布在  Java
关注(0)|答案(1)|浏览(123)

我有方法从BE获取数据
在这里

getChartsData(): any {
const documentStyle = getComputedStyle(document.documentElement);
const textColor = documentStyle.getPropertyValue('--text-color');
const textColorSecondary = documentStyle.getPropertyValue(
  '--text-color-secondary'
);
const surfaceBorder = documentStyle.getPropertyValue('--surface-border');

this._testFileService.getFileData().subscribe((result) => {
  console.log(result);
  console.log(
    result.map((obj: any) => {
      obj.city;
    })
  );

}
console.log(result);返回369个对象的数组的问题
但是console.log( result.map((obj: any) => { obj.city; }) );
返回未定义
我该怎么解决这个问题?
我的问题是

hmae6n7t

hmae6n7t1#

您缺少一个return语句:

this._testFileService.getFileData().subscribe((result) => {
  console.log(result);
  console.log(
    result.map((obj: any) => {
      return obj.city; // <== don't forget the return 
    }
  );

你也可以缩短它,如果回报很简单,比如:

result.map((obj: any) => obj.city)

相关问题