You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从JavaScript数组中提取每个对象的title属性?

提取数组对象中的title属性方法

首先先修正你代码里的语法错误,这些错误会导致代码无法正常运行:

  1. JS关键字区分大小写,Let 要改成小写的 let
  2. 对象的键值对赋值要用 :,不是 =
  3. 字符串引号要用英文的 " 或 ',不要用中文引号
  4. 数组最后一个对象末尾不要加多余逗号

修正后的数组代码:

let books = [
  {title: "harry potter", author: "jk Rowling"},
  {title: "another title", author: "another author"},
  {title: "3rd title", author: "3rd author"}
];

用forEach打印每个title

你之前的forEach代码是打印整个对象,要获取每个对象的title属性,直接通过element.title访问即可,正确写法:

books.forEach(element => console.log(element.title));

你尝试的[books].title是错误写法:[books]是把整个原数组再包裹成一个新数组,这个新数组的元素是原books数组,而数组本身没有title属性,所以会报错,完全没必要这么写。

提取所有title到新数组(更推荐)

如果你的需求是把所有title提取成一个独立数组,用map方法更高效,它会返回一个新数组,包含每个对象的title:

const bookTitles = books.map(book => book.title);
console.log(bookTitles); // 输出:["harry potter", "another title", "3rd title"]

内容的提问来源于stack exchange,提问作者user2323

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 09:40:32