首页 > 解决方案 > 如何访问数组中对象的属性

问题描述

您如何打印数组中对象的所有属性。

例如我有这个数组

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)

给我未定义?您不能同时选择所有这些吗?

标签: javascriptarraysjavascript-objects

解决方案


您需要遍历数组

const textArray = todo.map(item => item.text)

推荐阅读