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

用对象的属性查找数组里的对象 #27

Open
lovelmh13 opened this issue Mar 29, 2020 · 0 comments
Open

用对象的属性查找数组里的对象 #27

lovelmh13 opened this issue Mar 29, 2020 · 0 comments

Comments

@lovelmh13
Copy link
Owner

[{"id":1101154,"name":"asd"}, {"id":1101067,"name":"abc"}] -> 找出id=1101067的对象 -> {"id":1101067,"name":"abc"}

function objFindInArray(array, key, val) {
  const res = array.find((obj) => {
    return obj[key] === val
  })
  return res
}

一个find就搞定,千万别遍历数组的每一项,又去遍历每一项的key:

      for (const chapter of this.chapterOptions) {
        Object.keys(chapter).find((item) => {
          if (chapter[item] === this.jump.chapterId) {
            this.currChapter = chapter
          }
        })
      }

这简直就是脱裤子放屁,没错 就是曾经的我...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant