首页 > 解决方案 > 从对象打印一定数量的属性

问题描述

假设我有一个这样的对象:

   const recordCollection = {
2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

我知道我可以像这样打印一个属性:

console.log (recordCollection[2548]['artist'];

我想知道是否有可能做这样的事情:

console.log (recordCollection[2548]['artist', 'tracks'];

我将如何打印艺术家和曲目?

标签: javascript

解决方案


它应该是:recordCollection[2548]['artist']

const recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};
console.log(recordCollection[2548]['artist']);


推荐阅读