-2

get response in function node.js

[
  {
    instrument_token: '12598786',
    exchange_token: '49214',
    tradingsymbol: 'AARTIIND21JAN1000CE',
    name: 'AARTIIND'   
  },
  {
    instrument_token: '12599810',
    exchange_token: '49218',
    tradingsymbol: 'AARTIIND21JAN1000PE',
    name: 'AARTIIND'
  }
]

How to get only instrument_token in another variable in node

const ObjData = JSON.stringify(response);
console.log(ObjData.instrument_token);

Result : undefined

  • 1
    First of all, please tag Javascript as well. – Hobbamok Jan 22 '21 at 11:06
  • 2
    I don't understand your question, can you clarify please, what exactly are you going to do with the array of objects ? – Max Jan 22 '21 at 11:07
  • 1
    Does this answer your question? [How to loop through an array containing objects and access their properties](https://stackoverflow.com/questions/16626735/how-to-loop-through-an-array-containing-objects-and-access-their-properties) – Hobbamok Jan 22 '21 at 11:21

4 Answers4

0

This isn't relevant to NodeJS because thats not a language. You want to ask for Javascript.

Since you don't have a JSON OBject, but instead an ARRAY of those, so you need to iterate over the array in a loop, and in it simply go = object.instrument_token

const yourDataArray = [];
function extractData(thatThingYouGetReturnedThere){
  for (let obj of thatThingYouGetReturnedThere){
    yourDataArray.push(obj.instrument_token)
  }
}

That way, after calling the function with your array, you have all the data you want stored as an array in yourDataArray

Hobbamok
  • 767
  • 1
  • 11
  • 21
0

OK so I think I follow your question.

Lets do this by example. If I have a variable foo which is an array of JSON objects:

let foo = [ 
            {
              instrument_token: '12598786',
              exchange_token: '49214',
              tradingsymbol: 'AARTIIND21JAN1000CE',
              name: 'AARTIIND'   
            },
            {
              instrument_token: '12599810',
              exchange_token: '49218',
              tradingsymbol: 'AARTIIND21JAN1000PE',
              name: 'AARTIIND'
            }
          ]

To get any individual key-value object pair out I need to specify the array number I want to access. So to get instrument_token from the first object I would do:

let bar = foo[0];                              // Now bar contains the first JSON object
let myInstrumentToken = bar.instrument_token;  // Instrument token from the first object

Similarly if I want the name from the second object I would do:

let bar = foo[1];       // Now bar contains the second JSON object of the array
let myName = bar.name;  // Name from the second object
Dharman
  • 30,962
  • 25
  • 85
  • 135
gruffmeister
  • 68
  • 1
  • 6
0
const data = [
  {
    instrument_token: '12598786',
    exchange_token: '49214',
    tradingsymbol: 'AARTIIND21JAN1000CE',
    name: 'AARTIIND'   
  },
  {
    instrument_token: '12599810',
    exchange_token: '49218',
    tradingsymbol: 'AARTIIND21JAN1000PE',
    name: 'AARTIIND'
  }
];
let tokenArray =[];
for (let obj of data){
    tokenArray.push(obj.instrument_token)
  }
console.log(tokenArray);  
//It will give you [ '12598786', '12599810' ]  
Hope this is what you want
Antier Solutions
  • 1,326
  • 7
  • 10
0

if you have :

response = [
  {
    instrument_token: '12598786',
    exchange_token: '49214',
    tradingsymbol: 'AARTIIND21JAN1000CE',
    name: 'AARTIIND'   
  },
  {
    instrument_token: '12599810',
    exchange_token: '49218',
    tradingsymbol: 'AARTIIND21JAN1000PE',
    name: 'AARTIIND'
  }
]

you can do this:

response.map(x => x = {instrument_token: x.instrument_token})

/* 
 #### output : 
[
{ instrument_token: "12598786" }, 
{ instrument_token: "12599810" } 
]
*/

Was this what you wanted?

niksolaz
  • 102
  • 10