js 对象数组中判断某一对象是否存在,获取下标和获取所查询的对象

博客围绕JS展开,重点涉及JS数组,尤其是对象数组查询方面的内容,属于信息技术领域中前端开发的知识范畴。

在这里插入图片描述

在 JavaScript 中,判断数组对象中某个值是否存在有多种方法,以下为你详细介绍: ### 使用 `array.indexOf` 方法 `indexOf` 方法可判断数组是否存在某个值,如果存在返回数组元素的下标,否则返回 -1。不过该方法通常适用于简单数组,对于数组对象,若要判断对象中某个属性值是否存在,需要配合一些处理。 ```javascript const arr = [1, 2, 3]; const index = arr.indexOf(2); if (index!== -1) { console.log('值存在'); } else { console.log('值不存在'); } // 对于数组对象判断某个属性值 const objArr = [ { id: 1, name: 'xiaoming' }, { id: 2, name: 'xiaobai' } ]; const targetId = 1; const targetIndex = objArr.map(item => item.id).indexOf(targetId); if (targetIndex!== -1) { console.log('对象的 id 值存在'); } else { console.log('对象的 id 值不存在'); } ``` ### 使用 `array.includes` 方法 `array.includes(searchElement[, fromIndex])` 方法判断数组是否存在某个值,如果存在返回 `true`,否则返回 `false`。它采用严格相等比较,能检测数组是否包含 `NaN`,且不会跳过缺失的索引。对于数组对象,同样需要先处理获取属性值数组判断[^4]。 ```javascript const arr = [1, 2, 3]; const hasValue = arr.includes(2); if (hasValue) { console.log('值存在'); } else { console.log('值不存在'); } // 对于数组对象判断某个属性值 const objArr = [ { id: 1, name: 'xiaoming' }, { id: 2, name: 'xiaobai' } ]; const targetId = 1; const hasTargetId = objArr.map(item => item.id).includes(targetId); if (hasTargetId) { console.log('对象的 id 值存在'); } else { console.log('对象的 id 值不存在'); } ``` ### 使用 `array.find` 方法 `array.find` 方法返回数组中满足提供的测试函数的第一个元素的值,若没有找到则返回 `undefined`。对于数组对象,可以直接根据属性值进行判断。 ```javascript const objArr = [ { id: 1, name: 'xiaoming' }, { id: 2, name: 'xiaobai' } ]; const targetId = 1; const foundObj = objArr.find(item => item.id === targetId); if (foundObj) { console.log('对象的 id 值存在'); } else { console.log('对象的 id 值不存在'); } ``` ### 使用 `array.some` 方法 `array.some` 方法测试数组中是不是至少有 1 个元素通过了被提供的函数测试。它返回的是一个布尔值。 ```javascript const objArr = [ { id: 1, name: 'xiaoming' }, { id: 2, name: 'xiaobai' } ]; const targetId = 1; const hasTarget = objArr.some(item => item.id === targetId); if (hasTarget) { console.log('对象的 id 值存在'); } else { console.log('对象的 id 值不存在'); } ``` ### 使用 `array.filter` 方法 `array.filter` 方法创建一个新数组,其包含通过所提供函数实现的测试的所有元素。若返回的数组长度大于 0,则表示存在该值。 ```javascript const objArr = [ { id: 1, name: 'xiaoming' }, { id: 2, name: 'xiaobai' } ]; const targetId = 1; const filteredArr = objArr.filter(item => item.id === targetId); if (filteredArr.length > 0) { console.log('对象的 id 值存在'); } else { console.log('对象的 id 值不存在'); } ``` ### 使用 `Set` 对象 可以先将数组对象的某个属性值提取出来,再利用 `Set` 对象的特性判断是否存在重复值,若 `Set` 的大小数组长度相等,则不存在重复值。 ```javascript function hasDuplicates(array) { return new Set(array).size!== array.length; } const objArr = [ { id: 1, name: 'xiaoming' }, { id: 2, name: 'xiaobai' } ]; const idArr = objArr.map(item => item.id); const hasDuplicateId = hasDuplicates(idArr); if (hasDuplicateId) { console.log('存在重复的 id 值'); } else { console.log('不存在重复的 id 值'); } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值