首页 VueJS json 判断值是否存在 vue

json 判断值是否存在 vue

作者:胡同里的砖头 围观群众:106 更新于:2024-06-07

可使用find方法或some方法,任选一种

let arr = [
{id: 1, name: 'foo'},
{id: 2, name: 'bar'},
{id: 3, name: 'baz'}
]
let result = arr.find(item => item.name === 'bar')
// 如果result不为undefined,则说明包含'name'为'bar'的对象
if (result) {
console.log('包含')
} else {
console.log('不包含')
}
let arr = [
{id: 1, name: 'foo'},
{id: 2, name: 'bar'},
{id: 3, name: 'baz'}
]
let hasBar = arr.some(item => item.name === 'bar')
if (hasBar) {
console.log('包含')
} else {
console.log('不包含')
}

  • 本文标题: json 判断值是否存在 vue
  • 文章分类:【VueJS】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8