简体   繁体   中英

Property 'map' does not exist on type 'Object' Angular

I have code in my dashboard.component.ts, just like this:

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. If you want to use map on an object, you can use lodash ;

so if you use 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] })));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM