javascript - 从对象打印一定数量的属性
问题描述
假设我有一个这样的对象:
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'];
我将如何打印艺术家和曲目?
解决方案
它应该是: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']);
推荐阅读
- java - Spark Streaming:在大量数据的情况下不启动 Foreach 写入
- vue.js - VueJS:路由混乱
- android - 数据更改后的Android FirebaseDatabase,值显示两次
- angular - 无法激活已激活的出口角 6
- reactjs - 当在 jsonObject 的属性上调用 setState 时,React 组件状态值未更新
- sql - jdbcTemplate 和原始 JSON 列
- sql - 访问 SQL 查询:按月分组,如果不存在也可以
- javascript - 截图并发送给带有node js的客户端
- android - android:列表视图获取选中项的方法
- azure - Azure 的自定义映像 VM 上没有 Internet