mardi 27 octobre 2020

Get all values in randomly selected JSON object

I'm retrieving a list of objects from a json call. They are quotes with values of 'Author' and 'Text'. Here is a small sample.

0:
author: "Thomas Edison"
text: "Genius is one percent inspiration and ninety-nine percent perspiration."
__proto__: Object
1:
author: "Yogi Berra"
text: "You can observe a lot just by watching."
__proto__: Object

Then I am selecting a random object from the list. I cant figure out how to get both values from the selected object. I can get text and author separately but I cant seem to get them both

these do work

data[ Math.floor(Math.random() * data.length) ]['text']
data[ Math.floor(Math.random() * data.length) ]['author']

I tried these and neither work

data[ Math.floor(Math.random() * data.length) ]['author','text']
data[ Math.floor(Math.random() * data.length) ]['author'],['text']

Thanks in advance




Aucun commentaire:

Enregistrer un commentaire