Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用 Object.entries() 解构出对象的键值对 #26

Open
JslinSir opened this issue Oct 24, 2022 · 0 comments
Open

用 Object.entries() 解构出对象的键值对 #26

JslinSir opened this issue Oct 24, 2022 · 0 comments
Labels
奇巧淫技 开发中遇到的一些奇巧淫技

Comments

@JslinSir
Copy link
Owner

我们可能会碰到一些这种场景,就是根据对象的键值对来处理逻辑的需求

通常,我会用 Object.keys 或者 Object.values 但是这两个方法只能迭代出 键的数组 或值的数组

Object.entries 有个优点就是可以迭代出 键值对数组

const oj = {
    name:'jslin',
    age:'16',
    sex:'男',
    bag:'xxx',
    0:'xxxx'
}
Object.entries(oj)

image

实践中,我们可以用 这个键值对数组更快捷高效的去完成特殊场景需求
如:

Object.entries(oj).map(([key,val])=>{
   console.log('key:',key,'val:',val)
})

Object.entries(oj).forEach(([key,val])=>{
   console.log('key:',key,'val:',val)
})

for(const [key,val] of Object.entries(oj)){
    console.log('key:',key,'val:',val)
    
}
@JslinSir JslinSir added the 奇巧淫技 开发中遇到的一些奇巧淫技 label Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
奇巧淫技 开发中遇到的一些奇巧淫技
Projects
None yet
Development

No branches or pull requests

1 participant