lokanath
lokanath

Reputation: 247

how to call the array fields inside JSON from node.js

In node.js app, In server.js file I am trying to the request and the print the values to console from an external api called Deckofcardsapi using the below code.

const  request = require('request');
request('https://deckofcardsapi.com/api/deck/new/draw/?count=2', function (error, response, body) {
const data = JSON.parse(body);
console.log('remaining :', data.remaining);
console.log('statusCode:', response && response.statusCode);
console.log('suit:', data.cards[suit]);
});

when i try data.cards[suit] i get an error as below.

console.log('suit:', data.cards[suit]);
ReferenceError: suit is not defined
at Request._callback (/Users/lokanathc/Projects/deckOfCards/server.js:48:37)
at Request.self.callback (/Users/lokanathc/Projects/deckOfCards/node_modules/request/request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (/Users/lokanathc/Projects/deckOfCards/node_modules/request/request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (/Users/lokanathc/Projects/deckOfCards/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)`

but when i use just data.cards i get the following output

remaining : 50
statusCode: 200
suit: [ { code: 'QC',
images:
 { png: 'https://deckofcardsapi.com/static/img/QC.png',
   svg: 'https://deckofcardsapi.com/static/img/QC.svg' },
value: 'QUEEN',
image: 'https://deckofcardsapi.com/static/img/QC.png',
suit: 'CLUBS' },
  { code: '5S',
images:
 { png: 'https://deckofcardsapi.com/static/img/5S.png',
   svg: 'https://deckofcardsapi.com/static/img/5S.svg' },
value: '5',
image: 'https://deckofcardsapi.com/static/img/5S.png',
suit: 'SPADES' } ]

What should i use to access the value of suit: 'SPADES'.

This is the below response from the API .

{
"success": true,
"cards": [
    {
        "image": "https://deckofcardsapi.com/static/img/KH.png",
        "value": "KING",
        "suit": "HEARTS",
        "code": "KH"
    },
    {
        "image": "https://deckofcardsapi.com/static/img/8C.png",
        "value": "8",
        "suit": "CLUBS",
        "code": "8C"
    }
],
"deck_id":"3p40paa87x90",
"remaining": 50
}

Upvotes: 2

Views: 1754

Answers (2)

lokanath
lokanath

Reputation: 247

To access the suit value which is inside the cards array, we need to access the array first with it index and then the name of required field name which is residing in the array. This worked for me.

console.log('suit:', data.cards[0].suit);

So for the below code,

const  request = require('request');
request('https://deckofcardsapi.com/api/deck/new/draw/?count=1', function (error, response, body) {
const data = JSON.parse(body);
console.log('remaining :', data.remaining); 
console.log('statusCode:', response && response.statusCode); 
console.log('suit:', data.cards[0].suit);
});

The output is:

remaining : 51
statusCode: 200
suit: HEARTS

This output is for count=1, if we are using count = >1, then we need to iterate the array using for loop.

Upvotes: 0

dboscanv
dboscanv

Reputation: 56

You should use filter method, example: data.cards.filter(card => card.suit == 'SPADES');

Documentation of filter method

EDITED:

If you want to access to all values of suit in a array of objects:

let suites = data.cards.map(card => card.suit); // suites is ['SPADES','HEARTS']

Upvotes: 4

Related Questions