简体   繁体   English

如何从 object 中提取信息?

[英]How do I pull information from an object?

I have this object and I'm trying to access information of the certain member whose button is clicked on.我有这个 object 并且我正在尝试访问单击其按钮的某个成员的信息。 To do that, I want to match the class name of the section of each members using card.dataset.name, which only contain's the first name each member.为此,我想使用 card.dataset.name 匹配每个成员部分的 class 名称,其中仅包含每个成员的名字。 But I am not sure how to check that it matches the object in the array.但我不确定如何检查它是否与阵列中的 object 匹配。

const beatles = {
  john: {
    name: 'John Lennon',
    image:'...',
    bio: `...`,
  },
  paul: {
    name: 'Paul McCartney',
    image:'...',
    bio: `...`,
  },
  george: {
    name: 'George Harrison',
    image:
      '...',
    bio: `...`,
  },
  ringo: {
    name: 'Ringo Starr',
    image: '...',
    bio: '...',
  },
};

I hope this is what you looking for:我希望这是你要找的:

const beatles = [
  john: {
    name: 'John Lennon',
    image:'...',
    bio: `...`,
  },
  paul: {
    name: 'Paul McCartney',
    image:'...',
    bio: `...`,
  },
  george: {
    name: 'George Harrison',
    image:
      '...',
    bio: `...`,
  },
  ringo: {
    name: 'Ringo Starr',
    image: '...',
    bio: '...',
  },
];

function buttonClicked(objectClicked) {
  beatles.forEach(person => {
    if(person.name === objectClicked.dataset.name) {
      //Do something.
    }
  })
}

Don't forget to change the brackets to be an array of objects!不要忘记将括号更改为对象数组!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM