gpt4 book ai didi

javascript - 类型 'map' Angular 上不存在属性 'Object'

转载 作者:行者123 更新时间:2023-12-03 00:13:07 26 4
gpt4 key购买 nike

我的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

我在编写这段代码时是否犯过任何错误?或者我只是忘记了什么?

最佳答案

native map 仅存在于数组中。如果你想在一个对象上使用map,你可以使用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] })));

关于javascript - 类型 'map' Angular 上不存在属性 'Object',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54633056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com