简体   繁体   中英

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. 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. But I am not sure how to check that it matches the object in the array.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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