javascript 使用节点js迭代json数组对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33478770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:31:47  来源:igfitidea点击:

Iterating json array objects using node js

javascriptarraysjsonnode.js

提问by Ananth

I have got a task to iterate through the complex json file that contains json array. I could not access the array object from the json file.

我有一个任务来遍历包含 json 数组的复杂 json 文件。我无法从 json 文件访问数组对象。

I need to access the particularly the class-name object from the json file.

我需要从 json 文件中访问特别是类名对象。

classdetail.json

类详细信息.json

[ [ {   "student" : [ 
     {
     "name" : "AAaa",
     "class-name" : "A",
     "grade-label" : "AA"   }, 
     {
     "name" : "AAbb",
     "class-name" : "A",
     "grade-label" : "AB"   }, 
     {
     "name" : "AAcc",
     "class-name" : "A",
     "grade-label" : "AB"   }, 
     {
     "name" : "AAdd",
     "class-name" : "B",
     "grade-label" : "AA"   } ],  
      "Average" : 2.5 },

      {   
     "student" : [ 
      {
     "name" : "BBaa",
     "class-name" : "B",
     "grade-label" : "AB"   }, 
      {
     "name" : "BBbb",
     "class-name" : "B",
     "grade-label" : "AA"   }, 
      {
     "name" : "BBcc",
     "class-name" : "B",
     "grade-label" : "AA"   }, 
      {
     "name" : "BBdd",
     "class-name" : "B",
     "grade-label" : "AA"   } ],   
      "Average" : 2.5 } ] ]

iterate.js

迭代.js

var fs = require('fs');
var express = require('express');
var http = require('http');
var publicApis;
var item;
var subItem;


classmem = JSON.parse(fs.readFileSync("classdetail.json", "utf8"));



for (item in classmem) {
  for (subItem in classmem[item]) {
     console.log(classmem[item][subItem]);
  }
}

采纳答案by Tomasz Jakub Rup

for (item in classmem) {
  for (subItem in classmem[item]) {
     var student = classmem[item][subItem].student;
     for (row in student) {
       console.log(student[row]['class-name']);
     }
  }
}

But read about Array.forEach.

但请阅读Array.forEach

回答by ali khabbaz

first check the value is array then access to the "class-name" value

首先检查值是数组然后访问“类名”值

for (item in classmem) {
    for (subItem in classmem[item]) {
        **if (typeof classmem[item][subItem] === 'object') {
            classmem[item][subItem].forEach(function (val, ind) {
                console.log(val['class-name']);
            });
        }**
    }
}

回答by Dan O

for...initerates over object properties in arbitrary order. It might not be what you want to use for an array, which stores items in a well-defined order. (though it should work in this case)

for...in以任意顺序迭代对象属性。它可能不是您想要用于以明确定义的顺序存储项目的数组的内容。(虽然它应该在这种情况下工作)

Try Array.forEach():

尝试Array.forEach()

// iterate over `classmem`
classmem.forEach(function(element, index, array) {
  // iterate over classmem[index], which is an array too
  element.forEach(function(el, idx, arr) {
    // classmem[index][idx] contains objects with a `.student` property
    el.student.forEach(function(e, i, a) {
      console.log(e["name"], e["class-name"], e["grade-label"]);
    });
  });
});