最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Iterating json array objects using node js - Stack Overflow

programmeradmin1浏览0评论

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

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

classdetail.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

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]);
  }
}

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

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

classdetail.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

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]);
  }
}
Share Improve this question asked Nov 2, 2015 at 13:30 AnanthAnanth 451 gold badge1 silver badge6 bronze badges 3
  • 2 What's the error you're getting? – Andrius Commented Nov 2, 2015 at 13:35
  • Everything works fine. classmem[item][subItem].student is a student array. – Tomasz Jakub Rup Commented Nov 2, 2015 at 13:42
  • im not getting any error but i cant able access the particular json object – Ananth Commented Nov 3, 2015 at 6:32
Add a ment  | 

3 Answers 3

Reset to default 2
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.

for...in iterates 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)

Try 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"]);
    });
  });
});

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']);
            });
        }**
    }
}
发布评论

评论列表(0)

  1. 暂无评论