CRYPTO NEWS

How do I destructure this Object?

const WebSocket = require('ws')
let socket = new WebSocket('wss://ftx.com/ws/');


const dataObject = {'op': 'subscribe', 'channel': 'trades', 'market': 'BTC-PERP'};

socket.onopen = function() {

    // Send an initial message
    socket.send(JSON.stringify(dataObject));
    console.log('Connected')

    messages = []

    socket.onmessage = (msg) => {
        const priceData = JSON.parse(msg.data)
        messages.push(priceData.data)
        console.log(priceData.data)

    }



    // Listen for socket closes
    socket.onclose = function(event) {
        console.log('Client notified socket has closed', event);
    };

    // To close the socket....
    // socket.close()

};
[nodemon] starting `node socket-ftx.js`
Connected
undefined
[
  {
    id: 5014962233,
    price: 19280,
    size: 0.03,
    side: 'sell',
    liquidation: false,
    time: '2022-09-22T20:24:52.011309+00:00'
  },
  {
    id: 5014962234,
    price: 19280,
    size: 0.02,
    side: 'sell',
    liquidation: false,
    time: '2022-09-22T20:24:52.011309+00:00'
  },
  {
    id: 5014962235,
    price: 19280,
    size: 0.0047,
    side: 'sell',
    liquidation: false,
    time: '2022-09-22T20:24:52.011309+00:00'
  }
]

I am trying to store the price and size elements into an array, however I can’t parse that array from the response.

priceData.data[0].price does not work. It results in a TypeError. I have tested (typeof passed priceData.data) and it logs as an object and not an array. I am confused here…

EDIT**

    console.log('Connected')
    socket.onmessage = (msg) => {
        const priceData = JSON.parse(msg.data)
        
        priceArray = []
        
        if (Array.isArray(priceData.data) /* you might add an additional check if the msg contains an identifier of the data type */) {
            const price = priceData.data[0].price
            if (price > 0) {
                priceArray.push(price)
                console.log(priceArray[0])  
            }
        }
    }

This worked! Thanks for the help!

Hendrick Douglass

#Hendrick Douglass #TheSupremeTeam Artist Name: Hendrick Douglass Who are you? I’m an enigma, plain & simple. I’m the “weird” or “different” artist from Harlem. I switch sounds and styles like I switch clothes so I guess it’s hard to get a full reading on me at times even from me. I’m always shooting for the::Listen

#Hendrick Douglass #TheSupremeTeam
Artist Name: Hendrick Douglass

Who are you?

I’m an enigma, plain & simple. I’m the “weird” or “different” artist from Harlem. I switch sounds and styles like I switch clothes so I guess it’s hard to get a full reading on me at times even from me. I’m always shooting for the moon in music and art and Kanye really started that mindset for me. I don’t follow trends, I make my own and don’t care who’s on the wave cause it’s therapeutic to me & im just the vessel for a higher being to speak thru.

Where are you from?

I’m from Harlem, New York also known as HarlemWorld. The music here is and has always been on the pulse of mega energy, good vibes, storytelling & lyrical prowess. Big L, QTip, Puff, Cam, Mase, Jim Jones, A$AP Rocky, Ferg & Nast, Biz Markie, Teyana Taylor the list goes on but you get the idea. We the innovators and tastemakers & I’m continuing the trend for the town.

How can we follow you?

All my socials and music is connected in 1 great place for EVERYONE TO GET IN TUNE!
Linktr.ee/Hendrickdouglass

Song Title: The Zone

Listen to Hendrick Douglass:

Source: https://supremepr.us/

How do I destructure this Object?

Shopping cart
There are no products in the cart!
Continue shopping
0