首页 >> js开发 >> js代码JavaScript find()方法及返回数据实例
js代码JavaScript find()方法及返回数据实例
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
顾名思义就是 查找传入的对应数据当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。
如果没有符合条件的元素返回 undefined
// 定义一个需要被查找的数组
var memoList = [{
id: 1, name: '1'
},{
id: 2, name: '2'
},{
id: 3, name: '3'
}]
// 用 editItem 变量将 查找出来的数据进行接收
var editItem = memoList.find((ele) => {
return ele.id == 2
})
// 打印 editItem 得到的结果是 {id: 2, name: '2'}
// 定义一个需要被查找的数组
var memoList = [{
id: 1, name: '1'
},{
id: 2, name: '2'
},{
id: 3, name: '3'
}]
// 用 editItem 变量将 查找出来的数据进行接收
var editItem = memoList.find((ele) => {
return ele.id == 2
})
// 打印 editItem 得到的结果是 {id: 2, name: '2'}这看起来和没什么区别呀。。。但是~~
// 我们修改 接收后的值 editItem 的值
editItem.name = '我是修改后的'
// 我们再打印一下 editItem
console.log( editItem )
// {id: 2, name: '我是修改后的'}
// 我们修改 接收后的值 editItem 的值
editItem.name = '我是修改后的'
// 我们再打印一下 editItem
console.log( editItem )
// {id: 2, name: '我是修改后的'}感觉很正常嘛~然后我们再打印一下 被查找的数据 memoList
console.log(memoList)
// [{id: 1, name: "1"}, {id: 2, name: "我是修改后的"}, {id: 3, name: "3"}]
// 看得没 memoList 内的数据也被修改了
console.log(memoList)
// [{id: 1, name: "1"}, {id: 2, name: "我是修改后的"}, {id: 3, name: "3"}]
// 看得没 memoList 内的数据也被修改了这里就反应出 fine() 方法返回的结果内存指向依然是 memoList 所指向的内存地址所有这里返回的是浅拷贝的数据注意: find() 对于空数组,函数是不会执行的。注意: find() 并没有改变数组的原始值。浏览器支持
浏览器支持表格中的数字表示支持该方法的第一个浏览器版本号。
方法
find()
45.0
12.0
25.0
7.1
32.0
方法
find()
45.0
12.0
25.0
7.1
32.0
方法
方法
find()
45.0
12.0
25.0
7.1
32.0
find()45.012.025.07.132.0注意: IE 11 及更早版本不支持 find() 方法。array.find(function(currentValue, index, arr),thisValue)
参数
参数
描述
function(currentValue, index,arr)
必需。数组每个元素需要执行的函数。
函数参数:
参数
描述
currentValue
必需。当前元素
index
可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
thisValue
可选。 传递给函数的值一般用 “this” 值。
如果这个参数为空, “undefined” 会传递给 “this” 值
参数
描述
function(currentValue, index,arr)
必需。数组每个元素需要执行的函数。
函数参数:
参数
描述
currentValue
必需。当前元素
index
可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
thisValue
可选。 传递给函数的值一般用 “this” 值。
如果这个参数为空, “undefined” 会传递给 “this” 值
参数
描述
参数描述
function(currentValue, index,arr)
必需。数组每个元素需要执行的函数。
函数参数:
参数
描述
currentValue
必需。当前元素
index
可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
function(currentValue, index,arr)必需。数组每个元素需要执行的函数。
函数参数:
参数
描述
currentValue
必需。当前元素
index
可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
参数
描述
currentValue
必需。当前元素
index
可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
参数
描述
currentValue
必需。当前元素
index
可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
参数
描述
参数描述
currentValue
必需。当前元素
currentValue必需。当前元素
index
可选。当前元素的索引值
index可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
arr可选。当前元素所属的数组对象
thisValue
可选。 传递给函数的值一般用 “this” 值。
如果这个参数为空, “undefined” 会传递给 “this” 值
thisValue可选。 传递给函数的值一般用 “this” 值。
如果这个参数为空, “undefined” 会传递给 “this” 值以上就是本文的全部内容,希望对大家的学习有所帮助。
如果没有符合条件的元素返回 undefined
// 定义一个需要被查找的数组
var memoList = [{
id: 1, name: '1'
},{
id: 2, name: '2'
},{
id: 3, name: '3'
}]
// 用 editItem 变量将 查找出来的数据进行接收
var editItem = memoList.find((ele) => {
return ele.id == 2
})
// 打印 editItem 得到的结果是 {id: 2, name: '2'}
// 定义一个需要被查找的数组
var memoList = [{
id: 1, name: '1'
},{
id: 2, name: '2'
},{
id: 3, name: '3'
}]
// 用 editItem 变量将 查找出来的数据进行接收
var editItem = memoList.find((ele) => {
return ele.id == 2
})
// 打印 editItem 得到的结果是 {id: 2, name: '2'}这看起来和没什么区别呀。。。但是~~
// 我们修改 接收后的值 editItem 的值
editItem.name = '我是修改后的'
// 我们再打印一下 editItem
console.log( editItem )
// {id: 2, name: '我是修改后的'}
// 我们修改 接收后的值 editItem 的值
editItem.name = '我是修改后的'
// 我们再打印一下 editItem
console.log( editItem )
// {id: 2, name: '我是修改后的'}感觉很正常嘛~然后我们再打印一下 被查找的数据 memoList
console.log(memoList)
// [{id: 1, name: "1"}, {id: 2, name: "我是修改后的"}, {id: 3, name: "3"}]
// 看得没 memoList 内的数据也被修改了
console.log(memoList)
// [{id: 1, name: "1"}, {id: 2, name: "我是修改后的"}, {id: 3, name: "3"}]
// 看得没 memoList 内的数据也被修改了这里就反应出 fine() 方法返回的结果内存指向依然是 memoList 所指向的内存地址所有这里返回的是浅拷贝的数据注意: find() 对于空数组,函数是不会执行的。注意: find() 并没有改变数组的原始值。浏览器支持
浏览器支持表格中的数字表示支持该方法的第一个浏览器版本号。
方法
find()
45.0
12.0
25.0
7.1
32.0
方法
find()
45.0
12.0
25.0
7.1
32.0
方法
方法
find()
45.0
12.0
25.0
7.1
32.0
find()45.012.025.07.132.0注意: IE 11 及更早版本不支持 find() 方法。array.find(function(currentValue, index, arr),thisValue)
参数
参数
描述
function(currentValue, index,arr)
必需。数组每个元素需要执行的函数。
函数参数:
参数
描述
currentValue
必需。当前元素
index
可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
thisValue
可选。 传递给函数的值一般用 “this” 值。
如果这个参数为空, “undefined” 会传递给 “this” 值
参数
描述
function(currentValue, index,arr)
必需。数组每个元素需要执行的函数。
函数参数:
参数
描述
currentValue
必需。当前元素
index
可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
thisValue
可选。 传递给函数的值一般用 “this” 值。
如果这个参数为空, “undefined” 会传递给 “this” 值
参数
描述
参数描述
function(currentValue, index,arr)
必需。数组每个元素需要执行的函数。
函数参数:
参数
描述
currentValue
必需。当前元素
index
可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
function(currentValue, index,arr)必需。数组每个元素需要执行的函数。
函数参数:
参数
描述
currentValue
必需。当前元素
index
可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
参数
描述
currentValue
必需。当前元素
index
可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
参数
描述
currentValue
必需。当前元素
index
可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
参数
描述
参数描述
currentValue
必需。当前元素
currentValue必需。当前元素
index
可选。当前元素的索引值
index可选。当前元素的索引值
arr
可选。当前元素所属的数组对象
arr可选。当前元素所属的数组对象
thisValue
可选。 传递给函数的值一般用 “this” 值。
如果这个参数为空, “undefined” 会传递给 “this” 值
thisValue可选。 传递给函数的值一般用 “this” 值。
如果这个参数为空, “undefined” 会传递给 “this” 值以上就是本文的全部内容,希望对大家的学习有所帮助。
相关文章:
- js解决父组件将子组件作为弹窗调用只执行一次created的问题js大全
- js小程序实现简单语音聊天的示例代码js大全
- js浅谈Vuex的this.$store.commit和在Vue项目中引用公共方法js大全
- jsvue实现用户长时间不操作自动退出登录功能的实现代码js大全
- jsJS使用Chrome浏览器实现调试线上代码js大全
- jsVue自动构建发布脚本的方法示例js大全
- js代码javascript递归函数定义和用法示例分析
- jsvue在响应头response中获取自定义headers操作js大全
- jsvue中全局路由守卫中替代this操作(this.$store/this.$vux)js大全
- js微信小程序动态评分展示/五角星展示/半颗星展示/自定义长度展示功能的实现js大全