如何在没有特定键的情况下从JSON对象获取值

时间:2018-06-14 19:37:32

标签: javascript arrays json console.log

我有以下JSON对象,我想使用javascript在我的开发控制台日志中仅获取某些值。我试过但在代码下面,但我不知道如何循环数组数组。任何人都可以建议我如何实现这一目标。

var infoJSON;
for(key in myClass) {
  infoJSON = myClass[key];
  console.log(infoJSON);
}




var myClass= {
   "Subjects":"3",
   "Subject":{
      "maths":{
         "subject_id":"1",
         "subject_level":"easy",
         "marks":"90"
      },
      "english":{
         "subject_id":"2",
         "subject_level":"medium",
         "marks":"80"
      },
      "physics":{
         "subject_id":"3",
         "subject_level":"tough",
         "marks":"70"
      }
   },
   "Average": "80"
};

我正在尝试编写JavaScript函数,以下面给出的格式在浏览器开发工具控制台中输出主题总数,每个主题带有标记和平均标记。

Subjects: 3
- maths (90)
- english (80)
- physics (70)
Average: 80

代码应该适用于具有相同结构的任何JSON对象,因此不希望使用硬编码密钥(例如,数学,物理)

3 个答案:

答案 0 :(得分:0)

这可以使用简单的for inObject.keys()完成。

尝试以下方法:

var myClass= {"Subjects":"3","Subject":{"maths":{"subject_id":"1","subject_level":"easy","marks":"90"},"english":{"subject_id":"2","subject_level":"medium","marks":"80"},"physics":{"subject_id":"3","subject_level":"tough","marks":"70"}},"Average":"80"};

for(key in myClass){
  if(myClass[key].constructor.toString().indexOf("Object") > 0){
      Object.keys(myClass[key]).forEach((k)=>{
        console.log(k +" - "+ myClass[key][k].marks);
      });
   } else{
    console.log(key +" - " +myClass[key]);
   }
}

答案 1 :(得分:0)

这只是一个检查你在课堂上迭代的问题。如果键是一个字符串,你可以简单地打印它,如果它不是,你可以迭代它,并打印那个

var myClass= {"Subjects":"3","Subject":{"maths":{"subject_id":"1","subject_level":"easy","marks":"90"},"english":{"subject_id":"2","subject_level":"medium","marks":"80"},"physics":{"subject_id":"3","subject_level":"tough","marks":"70"}},"Average":"80"};

for (let key in myClass) {
  let value = myClass[key];
  if (typeof value === 'string') {
    console.log( `${key}: ${value}` );
    continue;
  }
  console.log( Object.keys( value ).map( k => `- ${k} (${value[k].marks})` ).join('\n') );
}

// if you want it in one log output
console.log( Object.keys( myClass ).reduce( (result, key) => {
  if (typeof myClass[key] === 'object') {
    let value = myClass[key];
    return result.concat( Object.keys( value ).map( k => `- ${k} (${value[k].marks})` ) );
  }
  result.push( `${key}: ${myClass[key]}` );
  return result;
}, [] ).join('\n') );

答案 2 :(得分:0)

主题和平均值始终处于相同位置,因此您需要对其进行硬编码。然后你必须继续数组来确定其他字段:

var myClass= {
   "Subjects":"3",
   "Subject":{
      "maths":{
         "subject_id":"1",
         "subject_level":"easy",
         "marks":"90"
      },
      "english":{
         "subject_id":"2",
         "subject_level":"medium",
         "marks":"80"
      },
      "physics":{
         "subject_id":"3",
         "subject_level":"tough",
         "marks":"70"
      }
   },
   "Average": "80"
};
var infoJSON;
console.log('Subjects: ' + myClass['Subjects']);
for(key in myClass['Subject']) {
  console.log('- ' + key + ' (' + myClass['Subject'][key]['marks'] + ')');
}
console.log('Average: ' + myClass['Average']);