https://i.sstatic.net/TKkcV.jpgI need help with updating properties within an object array. Below is my code for reference. I have an object array consisting of two arrays, where the first one contains attribute "first" and the second one contains "last". When a specific name (either first or last) is clicked on, the function will loop through the object array and update the value of the matching property (tempFirst, tempLast). Although I've only declared it within the onHandle function here, in my actual project, I'm passing these parameters.
For example: if (first === first), then change the value of that property.
import logo from './logo.svg';
import './App.css';
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.state = {
objectTemp: []
};
}
componentDidMount(){
let objectArray = [
{
name: "hello",
first: 77
},
{
name: "world",
last: 66
},
]
this.setState({
objectTemp: objectArray
})
}
onHandle = () => {
let tempFirst = "first";
let tempLast = "last";
let storeState = [...this.state.objectTemp];
// Here I want to check if either tempFirst or tempLast is equal to the property in the object array
for(let i = 0; i < storeState.length; i++){
//if(storeState[i] === tempLast){
console.log(...storeState[i]);
//}
}
}
render() {
console.log(this.state);
return <button onClick={this.onHandle}>VIEW</button>;
}
}
export default App;