这里介绍一些在写算法的过程中总结的常用的操作数组的方法:
新建一个数组对象,便于后续调试
const peoples=[
{
name:'sherlock',age:23},
{
name:'david',age:45},
{
name:'tom',age:34},
{
name:'jerry',age:16},
{
name:'rita',age:103}
]
以下的方法均基于此数组进行操作
1.flier()
Array.prototype.fliter(),返回值是一个新的、由通过测试的元素组成的数组,如果没有任何数组元素通过测试,则返回空数组。
const peopleFliter=peoples.filter((names)=>{
return names.age<=50
})
console.log(peopleFliter)
2.map()
对象保存键值对,并且能够记住键的原始插入顺序。任何值(对象或者原始值) 都可以作为一个键或一个值。所有的 Map
对象实例都会继承 Map.prototype
map在实际使用常用于数组的去重等操作,由于js中没有Hashmap,但Hashmap其实是map的底层实现,通过map.has()和map.set()的配合可以实现去除数组重复值的效果。map这里只是简单的介绍下,后续还需更细致的去理解
const peopleNames=peoples.map((names)=>{
return names.name
})
console.log