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

如何从含index属性的对象数组中提取price值生成新数组?

提取对象数组中的price值生成新数组

给定如下对象数组:

let arr = [
   {
     index: 1,
     price: "24.99"
   },
   {
     index: 2,
     price: "24.95"
   },
   {
     index: 3,
     price: "20.95"
   },
]

要转换为仅包含price值的数组:

["24.99", "24.95", "20.95"]

实现方法

方法一:使用Array.map()(推荐)

map()方法会遍历数组的每个元素,返回你指定的结果,是处理这类转换最简洁的方式:

const priceArr = arr.map(item => item.price);
console.log(priceArr); // 输出:["24.99", "24.95", "20.95"]

方法二:使用Array.forEach()手动构建数组

如果需要更基础的遍历方式,可以用forEach循环逐个提取值并添加到新数组:

const priceArr = [];
arr.forEach(item => {
  priceArr.push(item.price);
});
console.log(priceArr); // 输出:["24.99", "24.95", "20.95"]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 13:55:14