简体   繁体   English

从JSON文件接收到JavaScript对象后正确访问它

[英]Accessing JavaScript object correctly after receiving it from JSON file

I have a JSON file, which contains some data (in this case: 4 different items. AFTER I parse the JSON file I get the output seeing below. Now I have a javascript object, am I correct? 我有一个JSON文件,其中包含一些数据(在这种情况下:4个不同的项目。解析完JSON文件后,我将看到下面的输出。现在我有一个javascript对象,对吗?

I want to access each Item now, and I want to add every item to a plain Object. 我现在想访问每个项目,并且想将每个项目添加到一个普通对象中。 So how do I add it to the plain object? 那么如何将其添加到普通对象中呢? It should look like that I think: 我认为应该是这样的:

obj = [
    { id: 0, value: 'cas', date: '', tags: '' },
    { id: 1, value: '', date: 'vcxyv', tags: '' },
    ...
]

(afterward, I'm rendering these object to get a list on my web application) (之后,我将渲染这些对象以在Web应用程序上获取列表)

在此处输入图片说明

ok, thank u guys. 好的,谢谢你们。 I got it! 我知道了!

var obj = require('./expenses.json').expArray;
console.log(obj);

for (var key in obj) {
if (obj.hasOwnProperty(key)) {

    expArr[key] = obj[key];
   }
}
console.log(expArr);

here i my output now in the style of @tvankith 在这里,我现在以@tvankith的样式输出

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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