[英]Property 'map' does not exist on type 'Object' Angular
I have code in my dashboard.component.ts, just like this: 我在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;
})
And the import library: 和导入库:
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';
Are there any mistakes i have made while writing this code? 我在编写此代码时是否犯了任何错误? or have i just forget something? 还是我只是忘记了什么?
native map
is only existing on Array. 本机map
仅存在于Array上。 If you want to use map on an object, you can use lodash
; 如果要在对象上使用map,可以使用lodash
;
so if you use lodash 所以如果你用lodash
import { map } from 'lodash';
result = map(temp, o => ids.map(([key, id]) => ({ id, content: o[key] })));
otherwise you must loop throught the key list of the object, like 否则,您必须遍历对象的键列表,例如
result = Object.keys(temp).map(o => ids.map(([key, id]) => ({ id, content: temp[o][key] })));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.