0

I have an object defined like this:

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);
  }

I have the variable

console.log(item);// output {Dogs:[{name: "lofi"}]}  

I don't know how to verify which property is in the item (Dogs or Cats) so that I can update the object items{} to make it becоme in my example like this:

items{Dogs:[{name: "lofi"}], Cats:[]}
3
  • items.Dogs.push({name: 'lofi'})? Commented Jun 5, 2018 at 1:42
  • @RobbyCornelissen, the item's value can change, instead of output {Dogs: [{name: "lofi"}]} it could be {Cats: [{name:"tom"}]} Commented Jun 5, 2018 at 1:55
  • @E.Mohammed: Please try out my solution and let me know if it works for you Commented Jun 6, 2018 at 7:38

3 Answers 3

1
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)
})

You can use Object.keys to get all the keys and assuming your item has only one variable at a time, we set with index 0. Storing it in a variable so that during setState we will know which array in exact we going to push the item into

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry to be late to answer, it's working using your suggestion but with a little modification, to get the animaleObj I didn't use the keys let animalObj = item[whichAnimal]; // {name: 'lofi'} because there is only one object in it every time.
Its ok since StackOveflow is just a site to get the idea on achieving what you wanted to anyway. Have fun!
1

When you add "lofi" to items.Dogs, you should do like this:

this.state.items.Dogs.push({name:'lofi'})

The console output shows that you made the Cats disappear, so I wondered that you override the items state like:

this.state.items = {Dogs:[{name:'lofi'}]}

If I guess wrong, please let me know.


Edit:

Pass the values as parameter is like below:

add category:

var new_category = 'Birds';
this.state.items[new_category] = [];

add item to category:

var category = 'Birds';
var new_item = 'dori';
this.state.items[category].push({name: new_item });

4 Comments

But the items can change and I can have more than just Dogs and Cats, how I can access the items by the property that is the same in the item
If you want to have birds, do like so: this.state.items.birds = []; this.state.items.birds.push({name:'dori'});
Or you want to output only dogs, you can do like: console.log({Dogs: this.state.items.Dogs});
the items properties aren't predifiend, so I need a way to get the property's name of object as a variable that can change.
0

Since, this is a react app you want to use 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});
    }
}

In react don't try to change the state directly. Also, its always a best practice to copy the object first because if you change the original state object it can make your app behave unpredictable.

1 Comment

Thank you, but the variable 'item' can have object with different property, I have to verify if the property in the 'item' is a Dogs or Cats

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.