如何打印在“[object]”中的另一个数组中的数组的值

时间:2017-10-26 14:43:36

标签: javascript arrays node.js

所以我有一系列“会话”:

var sessions = [];

function Session(title, speaker) {
    this.title = title;
    this.speaker = speaker;
    this.id = nextId;
    this.ratings = [];
    this.currentRatingId = 0;
    this.currentSessionState = SessionState.Created; //SessionState is an enum
}

会议有一系列评分:

function Rating(evaluator,ratingValue) {
    this.ratingId;
    this.ratingValue = ratingValue;
    this.evaluator = evaluator;
    this.evaluatedSession;
}

如果我打印“sessions”数组,我会举例说明:

[Session {
title: 'Javasession',
speaker: 'JavaSpeaker ',
id: 1,
ratings: [[Object], [Object]],
currentRatingId: 2,
currentSessionState: 2
},
Session {
    title: 'C#Session',
    speaker: 'C#Speaker',
    id: 2,
    ratings: [[Object]],
    currentRatingId: 1,
    currentSessionState: 2
}]

如您所见,数组“session”中的数组不会打印对象。它只打印“[object]”..

是否有可能在不使用任何循环的情况下打印另一个数组中每个数组的值(for,foreach)....?

3 个答案:

答案 0 :(得分:1)

您可以使用JSON#stringify方法将其打印为string

<强> 实施例

&#13;
&#13;
const arr = [
  {
    subArr: [
        {
          subSubArr:[
            {
              a:1
            }
          ]
        }
    ]
  }
];
const res = JSON.stringify(arr);
console.log(res);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以在浏览器中使用console.dir()

const arr = [
  {
    subArr: [
        {
          subSubArr:[
            {
              a:1
            }
          ]
        }
    ]
  }
];
console.dir(arr, { depth: null });

答案 2 :(得分:0)

您可以将console.dir{ depth: null }

一起使用

使用简化数组:

let sessions =  [ Session: { ratings: [ {Rating: 1}, {Rating: 2} ] } ];

运行console.log(sessions);会导致:

[ { Session: { ratings: [Object] } } ]

运行console.dir(sessions, { depth: null})会导致:

[ { Session: { ratings: [ { Rating: 1 }, { Rating: 2 } ] } } ]