javascript - 如何访问数组中对象的属性
问题描述
您如何打印数组中对象的所有属性。
例如我有这个数组
const todo = [
{
text: "Water the plants",
completed: true
},
{
text: "Feed the dog",
completed: false
},
{
text: "Cook dinner",
completed: true
},
{
text: "Wash the dishes",
completed: false
},
{
text: "Clean the house",
completed: false
}
];
我是否能够同时打印/访问所有文本属性。
例如 - 如果我使用
console.log(todo.text)
给我未定义?您不能同时选择所有这些吗?
解决方案
您需要遍历数组
const textArray = todo.map(item => item.text)
推荐阅读
- javascript - 如何为java脚本中数组json对象的新属性赋值?
- mongodb - 聚合多个字段并计算平均mondodb
- c++ - 我写的合并排序的时间复杂度是多少
- c# - c#如何从它的派生类中取消订阅订阅事件的私有方法?
- python - 独角兽(uvicorn)。以编程方式或通过命令行运行?
- python - 检查数据库中的特定条目并获取它的 ID(如果存在)
- php - 根据字母对所有类别的 woocommerce 进行索引
- ssl - Let's Encrypt:SSL 证书对域有效,但对特定端口无效
- mongodb - 连接到 MongoDB 的最佳方式
- python - 使用 python 从 .tar.gz 文件中仅提取 jpg 文件