javascript - 我无法访问 JS 中的对象键
问题描述
在从父组件接收道具的反应组件中工作。道具是静态的(无需检查更新)并且接收正常。prop 是一个对象数组。该数组在控制台中记录正常,即使单个数组项数组 [x] 也已正确记录到控制台,但是如果我想访问特定键(例如“eventDate”),它会记录“无法读取未定义的事件日期” .
import React, { Component } from 'react';
// AUX COMP
import Moment from 'react-moment'; // --> https://momentjs.com/
import moment from 'moment';
// SERVICE API
import Calculations from '../../services/Calculations';
export default class EventsGraphic extends React.Component {
constructor(props){
super(props);
this.state = {
user : this.props.userID,
patientId : this.props.patID,
patientsEvents : this.props.events,
eventsSorted : [],
firstEventDate : '',
timeLineDays : 60,
}
}
_eventsGraphicData(){
let pEvts = [...this.props.events];
let eventsSorted = Calculations.sortByEventDate(pEvts);
console.log('pEvts', pEvts);
console.log('eventsSorted[0]', eventsSorted[0]);
// this prints to the console the object in the index 0 of the array correctly ONE OF THE KEYS IS eventDate
console.log('eventsSorted[0].eventDate = ', eventsSorted[0].eventDate);
// THIS LOGS "CAN NOT READ PROPERTY eventDate OF UNDEFINED ?!?!?!
return eventsSorted
}
render() {
return (
<div className="events-chart">
<p>DATA GOES HERE</p>
</div>
);
};
};
解决方案
您在 sortedEvent 而不是 Object 中有 Stringified 对象,当然在键上收到 undefined 。
推荐阅读
- html - 不变的内容应该在 index.html 或顶级 app.vue 文件中
- python - Jupyter Notebook 图片标题
- php - 我试图在 php 中创建一个循环,吐出下面的内容
- networking - Kubernetes 设置中集群的 pod 中的 Tcpdump(在 Minikube 设置中)
- git - git 在合并冲突后立即说“没有文件需要合并”
- c++ - 在c ++中将对象设置为等于同一类的构造函数是否有效?
- sql-server - 从三个表中获取 datediff
- machine-learning - 在数字信号中寻找模式
- javascript - 使用 python django + Javascript 嵌入推文
- c - 从一段单词创建哈希表和节点