属性“地图”在类型“对象”角度上不存在

时间:2019-02-11 14:45:07

标签: javascript angular typescript object

我在我的dashboard.component.ts中有代码,就像这样:

this.scheduleService.getShiftSchedule().subscribe((temp)=>{
  this.api = temp;
  var ids = [['user_id', 1], ['status', 2]],
  result = temp.map(o => ids.map(([key, id]) => ({ id, content: o[key] })));
  this.tablePresetData = result;
})

还有导入库:

import { Component, OnInit, ViewEncapsulation, ViewChild } from '@angular/core';
import { TicketService } from '../../ticket.service';
import { ScheduleService } from '../../schedule.service';
import {Chart} from 'chart.js';
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { map, filter, switchMap } from 'rxjs/operators';

enter image description here

编写此代码时是否犯了任何错误?还是我只是忘记了什么?

1 个答案:

答案 0 :(得分:1)

本地map仅存在于Array上。如果要在对象上使用地图,则可以使用lodash;

因此,如果您使用lodash

import { map } from 'lodash';
result = map(temp, o => ids.map(([key, id]) => ({ id, content: o[key] })));

否则,您必须遍历对象的键列表,例如

result = Object.keys(temp).map(o => ids.map(([key, id]) => ({ id, content: temp[o][key] })));