Lutfi Aldi
Lutfi Aldi

Reputation: 463

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';

enter image description here

Are there any mistakes i have made while writing this code? or have i just forget something?

Upvotes: 0

Views: 438

Answers (1)

Nguyen Phong Thien
Nguyen Phong Thien

Reputation: 3387

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] })));

Upvotes: 1

Related Questions