我想在陣營無法創建一個對象數組結構和增加值到數組
{
items:
{Dogs: [{name: "Snoopy"}, {name: "Lola"}, {name: "Sprinkles"}],
Cats: [{name: "Felidae"}, {name: "Garfiled"}, {name: "Cat in the Hat"}]
}
使用下面的函數來創建以下結構:
handleAddItem(s) {
var key = Object.keys(s)[0];
var value = s[key];
var allItems = {...this.state.items};
allItems[key] = allItems[key];
allItems[key].push({name: value});
var ourItems = {};
ourItems = allItems[key];
ourItems.push({name: value });
// console.log(ourItems);
// console.log(allItems);
this.setState({items: allItems});
}
爲什麼是關鍵值對被添加到狀態兩次?
是小寫字母和單數鍵「狗」和「貓」是拼寫錯誤嗎?不應該是複數「狗」,「貓」? –