如何提取数组里的某个属性

 时间:2026-02-16 02:39:38

1、一、indexOf()方法

// indexOf  返回数组中元素的下标,若没找到则返回-1

如何提取数组里的某个属性

2、var arr = ['lily','Anna','John','Trist','Hana'];

var index = arr.indexOf(3);  //返回 John

3、二、filter()方法

var arr = [

{name:"桌子",money:220},

{name:"板凳",money:60},

    {name:"床",money:1256}

]

var filterArr = arr.filter(function(elem,index,arr){

return elem.money>=200

})

//[{name:"桌子",money:220},{name:"床",money:1256}]

如何提取数组里的某个属性

4、filter() 方法将匹配元素集合缩减为匹配指定选择器的元素.该方法不改变原数组,返回的是筛选后满足条件的数组.

如何提取数组里的某个属性

5、拓展:ES6从数组和对象中获取数据的方法

如何提取数组里的某个属性

6、const names = ['Luke', 'Eva', 'Phil']

// 提取数组中第一个元素

const [first] = names;

console.log(first); // 'Luke'

// 提取数组中第一个和第二个元素

const [first, second] = names;

console.log(first, second); // 'Luke' 'Eva'

如何提取数组里的某个属性

7、const person = {

name: 'Luke',

age: '24',

facts: {

hobby: 'Photo',

work: 'Software Developer'

}

}

// 从 person 中提取 name 和 age

const {name, age} = person;

console.log(name, age); // 'Luke' '24'

// 提取嵌套值 person 中的 hobby

const {facts: {hobby}} = person;

console.log(hobby); // 'Photo'

如何提取数组里的某个属性

  • 亿图流程图制作软件搜索形状的操作流程
  • ajax如何将Date类型的数据显示成时分秒的格式
  • Photoshop 图层面板中常用功能的含义
  • 谷歌浏览器怎么设置网站是否能调用上网位置信息
  • javascript 中如何使用onmouseout
  • 热门搜索
    生汆丸子汤的做法 小便时尿道刺痛是怎么回事 蚂蚁上树的家常做法 鸡蛋花的做法 红薯的做法大全 五香花生米的做法 排骨做法大全 羊肉卷的做法 翘嘴鱼的做法 海绵蛋糕的做法