Javascript 参照数组中的对象
请说明如何引用(更改或删除)对象数组中的特定对象,例如Javascript 参照数组中的对象,javascript,reactjs,Javascript,Reactjs,请说明如何引用(更改或删除)对象数组中的特定对象,例如 var myArray = [ {id: 1}, {id: 2}, {id: 3}, {id: 4} ] //initial state for number set as an array state = { number: [] } //a function which runs any time a button called create is clicked increment = () => { this.s
var myArray = [ {id: 1}, {id: 2}, {id: 3}, {id: 4} ]
//initial state for number set as an array
state = {
number: []
}
//a function which runs any time a button called create is clicked
increment = () => {
this.setState({
numbers: [...this.state.number, {
id: Date.now()
}],
})
}
//the array looks like this: number=[{id:53432},{id:34534},{id:434553}]
//the array is passed on to a component using the map function
{
this.state.numbers.map(number => < Section key = {
number.id
}
/>)}
var myArray=[{id:1},{id:2},{id:3},{id:4}]
//设置为数组的数字的初始状态
状态={
编号:[]
}
//一个函数,可在单击名为“创建”的按钮时随时运行
增量=()=>{
这是我的国家({
编号:[…this.state.number{
id:Date.now()
}],
})
}
//数组如下所示:number=[{id:53432},{id:34534},{id:434553}]
//使用map函数将数组传递给组件
{
this.state.numbers.map(number=>)}
在部分
组件中,我有一个与数组的每个字段关联的按钮。
单击按钮时如何引用特定对象?例如,一个用于记录其按钮已按下的数组中对象的id的函数var myArray=[{id:1},{id:2},{id:3},{id:4}]
var myArray = [ {id: 1}, {id: 2}, {id: 3}, {id: 4} ]
handleClick(id){
console.log('clicked id is', id);
}
myArray.map((item) => {
<Button onClick={ () => this.handleClick(item.id)} />
})
handleClick(id){
log('clicked id为',id);
}
myArray.map((项目)=>{
this.handleClick(item.id)}/>
})
建议您学习一些基本的“todo”应用程序示例/教程,这些示例/教程可以在web上轻松找到。您询问的功能将包含在所有功能中。Stackoverflow并不是《感谢Anshul》中描述的“如何”教程服务。这让我知道了该怎么做