Javascript 使用属性'更新对象属性中的数组;姓名
我有一个对象定义如下:Javascript 使用属性'更新对象属性中的数组;姓名,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我有一个对象定义如下: import React, { Component } from 'react'; import './App.css'; class App extends Component { constructor() { super(); this.state = { lists: ["Dogs", "Cats"], items: {Dogs:[], Cats:[]} }; } handleAddItem(item) {
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor() {
super();
this.state = {
lists: ["Dogs", "Cats"],
items: {Dogs:[], Cats:[]}
};
}
handleAddItem(item) {
console.log(item);
}
items{Dogs:[{name: "lofi"}], Cats:[]}
this.state.items.Dogs.push({name:'lofi'})
我有变量
console.log(item);// output {Dogs:[{name: "lofi"}]}
我不知道如何验证项(Dogs
或Cats
)中的属性,以便更新对象项{},使其在我的示例中成为这样:
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor() {
super();
this.state = {
lists: ["Dogs", "Cats"],
items: {Dogs:[], Cats:[]}
};
}
handleAddItem(item) {
console.log(item);
}
items{Dogs:[{name: "lofi"}], Cats:[]}
this.state.items.Dogs.push({name:'lofi'})
当您向items.Dogs添加“lofi”时,您应该执行以下操作:
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor() {
super();
this.state = {
lists: ["Dogs", "Cats"],
items: {Dogs:[], Cats:[]}
};
}
handleAddItem(item) {
console.log(item);
}
items{Dogs:[{name: "lofi"}], Cats:[]}
this.state.items.Dogs.push({name:'lofi'})
控制台输出显示您使猫消失了,所以我想知道您是否覆盖了项的状态,如:
this.state.items = {Dogs:[{name:'lofi'}]}
如果我猜错了,请告诉我
编辑:
将值作为参数传递,如下所示:
添加类别:
var new_category = 'Birds';
this.state.items[new_category] = [];
var category = 'Birds';
var new_item = 'dori';
this.state.items[category].push({name: new_item });
将项目添加到类别:
var new_category = 'Birds';
this.state.items[new_category] = [];
var category = 'Birds';
var new_item = 'dori';
this.state.items[category].push({name: new_item });
当您向items.Dogs添加“lofi”时,您应该执行以下操作:
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor() {
super();
this.state = {
lists: ["Dogs", "Cats"],
items: {Dogs:[], Cats:[]}
};
}
handleAddItem(item) {
console.log(item);
}
items{Dogs:[{name: "lofi"}], Cats:[]}
this.state.items.Dogs.push({name:'lofi'})
控制台输出显示您使猫消失了,所以我想知道您是否覆盖了项的状态,如:
this.state.items = {Dogs:[{name:'lofi'}]}
如果我猜错了,请告诉我
编辑:
将值作为参数传递,如下所示:
添加类别:
var new_category = 'Birds';
this.state.items[new_category] = [];
var category = 'Birds';
var new_item = 'dori';
this.state.items[category].push({name: new_item });
将项目添加到类别:
var new_category = 'Birds';
this.state.items[new_category] = [];
var category = 'Birds';
var new_item = 'dori';
this.state.items[category].push({name: new_item });
因为,这是您想要使用的react应用程序setstate
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor() {
super();
this.state = {
lists: ["Dogs", "Cats"],
items: {Dogs:[], Cats:[]}
};
}
handleAddItem(item) {
console.log(item);
var copy = JSON.parse(JSON.stringify(this.state.items));
copy["Dogs"].push({name: "lofi"}];
this.setState({items: copy});
}
}
在react中,不要试图直接更改状态。此外,最好先复制对象,因为如果更改原始状态对象,可能会使应用程序的行为不可预测。因为,这是一个您想要使用的react应用程序设置状态
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor() {
super();
this.state = {
lists: ["Dogs", "Cats"],
items: {Dogs:[], Cats:[]}
};
}
handleAddItem(item) {
console.log(item);
var copy = JSON.parse(JSON.stringify(this.state.items));
copy["Dogs"].push({name: "lofi"}];
this.setState({items: copy});
}
}
const item = {Dogs:[{name: "lofi"}]}
let whichAnimal = Object.keys(item)[0]; //Dogs
let animalObj = item[whichAnimal][0]; //{name:'lofi'}
this.setState({
items: this.state.items[whichAnimal].push(animalObj)
})
在react中,不要试图直接更改状态。此外,最好先复制对象,因为如果更改原始状态对象,可能会使应用程序的行为不可预测
const item = {Dogs:[{name: "lofi"}]}
let whichAnimal = Object.keys(item)[0]; //Dogs
let animalObj = item[whichAnimal][0]; //{name:'lofi'}
this.setState({
items: this.state.items[whichAnimal].push(animalObj)
})
您可以使用Object.keys
获取所有键,并且假设您的项一次只有一个变量,我们将索引设置为0。将其存储在变量中,以便在setState
过程中,我们能够准确地知道要将项目推入哪个数组
您可以使用Object.keys
获取所有键,并且假设您的项一次只有一个变量,我们将索引设置为0。将其存储在一个变量中,以便在setState
期间,我们将知道要将项推入items.Dogs.push({name:'lofi'})
?@RobbyCornelissen,项的值可以更改,而不是输出{Dogs:[{name:'lofi}}}它可以是{Cats:[{name:'tom}}@E.Mohammed:请试用我的解决方案,让我知道它是否适用于您items.Dogs.push({name:'lofi'})
?@RobbyCornelissen,项目的值可以更改,而不是输出{Dogs:[{name:'lofi'}}}它可能是{Cats:[{name:'tom tom}}}}@E.Mohammed:请尝试我的解决方案,让我知道它是否对您有效。但是项目可以更改,我可以拥有的不仅仅是狗和猫,我如何通过项目中相同的属性访问项目如果您想要鸟类,请这样做:this.state.items.birds=[];this.state.items.birds.push({name:'dori});或者您只想输出狗,您可以这样做:console.log({dogs:this.state.items.dogs});items属性不是预先定义的,所以我需要一种方法来获取对象的属性名,作为一个可以更改的变量。但是这些项目可以更改,我可以拥有的不仅仅是狗和猫,我如何通过项目中相同的属性访问项目如果你想拥有birds,请这样做:this.state.items.birds=[];this.state.items.birds.push({name:'dori});或者您只想输出狗,您可以这样做:console.log({dogs:this.state.items.dogs});items属性不是预先定义的,因此我需要一种方法来获取对象的属性名称,作为一个可以更改的变量。谢谢,但是变量'item'可以有具有不同属性的对象,我必须验证'item'中的属性是Dogs
还是Cats
谢谢,但是变量'item'可以有具有不同属性的对象,我必须验证'item'中的属性是否是Dogs
或Cats
很抱歉回答晚了,它正在使用您的建议工作,但稍作修改,为了得到animaleObj,我没有使用键let animalObj=item[whichAnimal];/{name:'lofi'}
因为每次只有一个对象在其中。这没关系,因为StackOveflow只是一个网站,它可以让你了解如何实现你想要的。玩得高兴很抱歉,回答晚了,它正在使用您的建议,但稍作修改,以获得animaleObj,我没有使用键let animalObj=item[whichAnimal];//{name:'lofi'}
因为每次只有一个对象在其中。这没关系,因为StackOveflow只是一个网站,它可以让你了解如何实现你想要的。玩得高兴