在两个相等的JSON对象中,访问javascript中的值在一种情况下有效,而在另一种情况下返回未定义的值
我有两个相同的JSON对象,如下所示:在两个相等的JSON对象中,访问javascript中的值在一种情况下有效,而在另一种情况下返回未定义的值,javascript,json,reactjs,leaflet,Javascript,Json,Reactjs,Leaflet,我有两个相同的JSON对象,如下所示: JSON.stringify(obj1) == JSON.stringify(obj2); // returns true 此外,如果我将它们记录在控制台中,我可以正确地检查它们,并且一切正常。但是,由于某些原因,我无法访问obj2的值: console.log(obj1.type) // returns "FeatureCollection" console.log(obj2.type) // returns undefined json数据是从文件
JSON.stringify(obj1) == JSON.stringify(obj2); // returns true
此外,如果我将它们记录在控制台中,我可以正确地检查它们,并且一切正常。但是,由于某些原因,我无法访问obj2的值:
console.log(obj1.type) // returns "FeatureCollection"
console.log(obj2.type) // returns undefined
json数据是从文件加载的,如下所示(data.json):
区别在于:此场景发生在react应用程序中(在.jsx文件中)。obj1直接从.jsx文件中的文件加载。obj2被传递到react组件并通过this.props访问。它以前使用相同的语法从同一个文件加载。以下是两个文件:
传单-map.jsx:
import React from "react";
import * as geojson from "../assets/data.json";
export default class Map extends React.Component {
render(){
var obj1 = geojson;
var obj2 = this.props.geojson;
console.log(JSON.stringify(obj1) == JSON.stringify(obj2));
console.log(obj1.type);
console.log(obj2.type);
return (<div></div>);
}
}
从“React”导入React;
从“./assets/data.json”导入*作为geojson;
导出默认类映射扩展React.Component{
render(){
var obj1=geojson;
var obj2=this.props.geojson;
log(JSON.stringify(obj1)=JSON.stringify(obj2));
console.log(obj1.type);
console.log(obj2.type);
返回();
}
}
jsx
import React from "react";
import Map from "../../../components/leaflet-map";
import * as geojson from "../../../assets/data.json";
export default function LeafletMap() {
return (<Map geojson={geojson}></Map>);
}
从“React”导入React;
从“../../../components/传单贴图”导入贴图;
从“../../../assets/data.json”导入*作为geojson;
导出默认函数映射(){
返回();
}
发生了什么事?这太奇怪了,我都不知道从哪里开始找
更新:
我尝试了@TheMaster的提示,并将Map类定义更改为:
export default class Map extends React.Component {
constructor(props){
super(props);
console.log(this.props.geojson);
console.log(this.props.geojson.type);
}
render(){
return (<div></div>);
}
}
导出默认类映射扩展React.Component{
建造师(道具){
超级(道具);
log(this.props.geojson);
log(this.props.geojson.type);
}
render(){
返回();
}
}
结果:
显然,构造函数被调用了两次(?)
const p=Object.create(null);
const obj1=Object.create(p);
p、 type=“someType”;
obj1.txt=“val”;
常量obj2={txt:“val”};
console.log(JSON.stringify(obj1)=JSON.stringify(obj2));//真的
console.log(obj1.type);//某物
console.log(obj2.type);//未定义
感谢@TheMaster的评论,我现在知道我可以使用this.props.obj2.default
访问对象。这在实践层面上解决了我的问题,尽管我仍然不理解这种行为。映射类定义的重新定义说明了以下行为:
import React from "react";
import Map from "../../../components/leaflet-map";
import * as obj2 from "../../../assets/data.json";
export default function HelperMap() {
return (
<div>
<Map obj2={obj2}></Map>
</div>
);
}
从“React”导入React;
从“../../../components/传单贴图”导入贴图;
将*作为obj2从“../../../assets/data.json”导入;
导出默认函数HelperMap(){
返回(
);
}
从“React”导入React;
从“./assets/data.json”导入*作为obj1;
导出默认类映射扩展React.Component{
建造师(道具){
超级(道具);
控制台日志(obj1);
console.log(obj1.type);
console.log(this.props.obj2);
console.log(this.props.obj2.type);
log(this.props.obj2.default.type);
}
render(){
返回();
}
}
返回:
发布JSON数据,并尝试将构造函数和super(props)添加到Maptry
console.log(this.props.geojson);log(“type”在this.props.geojson中);log(Object.keys(geojson))console.log(this.props.geojson.type)
看起来也在default
中:this.props.geojson.default.type
谢谢@TheMaster。我刚刚找到了解决办法。如果我输入console.log(this.props.geojson.default.type),我会得到“FeatureCollection”。但是,为什么我需要在传递到react组件的对象中使用默认值,如果我直接在文件中创建对象,它在没有默认值的情况下工作?当我在控制台中检查这两个对象时,它们看起来一模一样。可爱,但我怀疑这与问题有关,正如更新的问题(部分)所提供的那样。更可能与它如何被传递到react组件有关。@PatrickRoberts yeah;这是一种可能性,但也不可能是两个对象的OwnProperty不同,并且具有相同的json.stringify
结果,因此它们可能会意外地操纵链中的某个位置的父属性type
import React from "react";
import Map from "../../../components/leaflet-map";
import * as obj2 from "../../../assets/data.json";
export default function HelperMap() {
return (
<div>
<Map obj2={obj2}></Map>
</div>
);
}
import React from "react";
import * as obj1 from "../assets/data.json";
export default class Map extends React.Component {
constructor(props){
super(props);
console.log(obj1);
console.log(obj1.type);
console.log(this.props.obj2);
console.log(this.props.obj2.type);
console.log(this.props.obj2.default.type);
}
render(){
return (<div></div>);
}
}