I have an object in React that I mapped and wanted to make it interact: I want it so when user clicks on the 'X' button next to an element, it would console.log the key of that clicked element.
Here is the object:
var obj = {
person1: {
name: 'iggy',
superPower: 'awesomeness',
favDonut: 'chocolate'
},
person2: {
name: 'iggy2',
superPower: 'stupendousness',
favDonut: 'glazed'
},
person3: {
name: 'iggy3',
superPower: 'amazingness',
favDonut: 'chocolate sprinkles'
}
};
The iteration looks like this:
X chocolate
X glazed
X chocolate sprinkles
If I click the x on glazed, I want it to console.log person2 because that's the parent object of glazed. If I click x on chocolate sprinkles, I want it to console.log person3.
Here is the fiddle: https://jsfiddle.net/iggyfiddle/b9rreoje/2/
The method that I am trying to figure out is the getPersonName function. What is the best way to allow user to click on the x and console.log the relevant object key?