Tick Twitch
Tick Twitch

Reputation: 566

Adding New Data to JS multidimensional Object

I am using an object in my react-redux app in reducer file.The object is like as below

tradings: {
        'buy': {
            data: []
        },
        'sell': {
            data: []
        },
        'total': {
            data: []
        }
    },

So,whenever i get a new dataset i want to push it into data array of any object ,Suppose i got buy and data:[time:234234, amount: 0.0123 ].So my new tradings object will look like this:

tradings: {
        'buy': {
            data: [[time:234234, amount: 0.0123 ], ....]
        },
        'sell': {
            data: []
        },
        'total': {
            data: []
        }
    },

How can i concat arrays into this array in object?

Upvotes: 0

Views: 91

Answers (2)

Mohammed Mortaga
Mohammed Mortaga

Reputation: 139

Assuming you have the following object:

tradings = {
        'buy': {
            data: []
        },
        'sell': {
            data: []
        },
        'total': {
            data: []
        }
    }

And your received data was:

data = {buy: {time:234234, amount: 0.0123 }}

You'll first need to grab the key in your data object and then push it to the desired array in your tradings object like the following:

key = Object.keys(data)[0];
tradings[key].data.push(data[key]);

fiddle: https://jsfiddle.net/9hsvd5pk/

Upvotes: 1

Max Alexander Hanna
Max Alexander Hanna

Reputation: 3871

tradings["buy"].data.push( YOUR ARRAY HERE )

to add arrays into that array, or create the array somewhere and add it via javascript :

tradings["buy"].data =  YOUR ARRAY HERE 

Upvotes: 0

Related Questions