So I'm new with Axios and I'm trying to make a Discord bot that takes info from an api website and uses it. The issue is the data given looks like:
{"status":1,"size":1,"result":[{"number":"9999", and so on.
How do I get the data inside of the [ ]? I tried:
var teamNumber = response.data.result.number
but it doesn't work.
I can get response.data.status, but not result.number
TLDR: how to get 9999 from {"result":[{"number":"9999", ?
result is an array; if you want to get the first element from it (and get that element's "number" field) you'd do:
console.log(response.data.result[0].number);
If you want to loop over them you'd do:
response.data.result.map(el => {
console.log(el.number);
});
Related
So I have an array in a firebase document called ActivityLikes. It looks like this:
The collection is like this below:
const db = fire.firestore();
db.collection("eventLikes")
.doc(eventID)
.set({
ActivityLikes: likes
})
I have another collection which has the word "Avoca" saved, and I have this right now and I basically want to pull the data that has "Avoca" in it. So like with the name "Avoca" i can also get the imgURL. I've tried a for loop to go inside the object that firebase returns but I guess my logic is due to the fact theres multiple value and key pairs inside of it so how would i get all of the data inside of index 1 because it has the name = "Avoca".
I have the activitylikes in a hook so I have it after calling it from firebase.
I've tried like:
for (var i=ActivityLikes.length; i--;) {
if (myArr[i].indexOf("Avoca")>=0) break;
}
I think my logic is off Im just not sure how to get all of the data inside of the index once the name matches
The other collection just looks like this: https://i.stack.imgur.com/TqhWd.png. I know how to call this from firebase, I just need to figure out the logic for getting all the data in the array in the ActivityLikes when the name "Avoca" is present
If you are looking just for the word "Avoca" then try using find():
// data is snapshot.data() after query
const avocaData = data.ActivityLikes.find(d => d.name === "Avoca")
Here avocaData will either be the first object from likes array where name is Avoca or undefined is there isn't any. After that you can use an if statement to check that and proceed with rest of the logic.
This is the output when printing my ${element}:
I wish to obtain the data of the first child of my ${element} that has a type: "text" and data valid duration (hours:minutes:seconds). In my case I want to obtain "5:01".
However, when trying to obtain all the children using ${element}.children() I get the following:
Basically there is no data and no type which I can check/obtain. What am I doing wrong?
From the log image I can see children property is an array not a function.
Following Code should get you the desired data.
element.children.find(child => child.type == 'text').data;
${element}.children().first().data()
I am currently working with the fetch API in JavaScript. I have a text file I'd like to read from called sample.txt. I would like to grab the different lines out of the text file and save it in an array so I can work with it. I've been searching for how to save it as an object, but I think I've been using the code for JSON and not for text. Please give any suggestions?
sample.txt
apple
banana
orange
grape
index.js
let fruitArray; //initialized new array
fetch('sample.txt') // fetch text file
.then((resp) => resp.text())
.then(data.split(/\r?\n/) = fruitArray)) //tried to assign each separate line as an element of fruitArray
Expected Output
fruitArray = ['apple', 'banana', 'orange', 'grape'];
let fruitArray; doesn't create a new array - you have to actually declare an array like [] for that. But it would be better to declare the array only once the response comes back, and then pass it around if needed.
.then accepts a function as a parameter, not a plain code block. The first parameter of the .then callback is the result of the resolution of the previous promise - which here is the full string.
When assigning (=), the left hand side needs to be a variable (or a property).
fetch('sample.txt') // fetch text file
.then((resp) => resp.text())
.then(data => {
const fruitsArray = data.split(/\r?\n/);
})
I have an application that i have built, these servers send data back and forth, the master server i have setup gives me the messages in this format.
{ 'register 9f624ed': '' }
I would like to be able to split this key into different sections.
I have used the .split method, this doesn't seem to work for this purpose, i get all sorts of errors.
I would like to be able to cut off { '
leave me register in a variable
and then leave me 9f624ed in a variable.
Thanks for any advice anyone can offer!
You can use Object.keys() to obtain the keys on the object and then use .split() on one of the keys. Here's working code in a snippet:
var data = { 'register 9f624ed': '' };
var keys = Object.keys(data);
var splits = keys[0].split(" ");
// show output in snippet window
document.write(JSON.stringify(splits));
I'm new to jQuery and just playing for fun. I have some code that I want to try to modify for my needs but the current js file is getting its data from google spreadsheets and then returning each item as objects. I don't use json to pass data from my server to jQuery so I'm wondering how I can convert json to objects.
The current way its doing it is(tabletop is the name of their js program that gets data from google docs):
Tabletop.init({
key: timelineConfig.key,
callback: setupTimeline,
wanted: [timelineConfig.sheetName],
postProcess: function(el){
//alert(el['photourl']);
el['timestamp'] = Date.parse(el['date']);
el['display_date'] = el['displaydate'];
el['read_more_url'] = el['readmoreurl'];
el['photo_url'] = el['photourl'];
}
});
I have added alerts all over the file and I think this is the area that gets the data and passes it on. I was thinking of trying to replace items in their object with objects from my json and see if it changes anything, but I'm unsure. Typrically I pass individual items via json,hashmaps, and lists, not sure how it works with objects or how to access objects(I simply call url's that I create for the requests, $("#user-history").load("/cooltimeline/{{ user.id }}");). But where do I start if I want to turn json data into objects?
If it helps, here's the demo of what I'm trying to do(but by having it use json data).
p.s. I'm really looking for the logic of how to complete what I'm trying to do and perhaps some ideas I'm missing so I can google them and learn.
Use use function JSON.parse(json) :) Or jQuery.parseJSON(json)
var json = '{"a":2}';
var object = JSON.parse(json);
alert(object.a);
You should see alert with message: 2
I don't realy know if I understand your comment, but maybe you want just do this:
postProcess: function(el){ //here el is JSON string
el = JSON.parse(el); // now el is an object
el.timestamp = Date.parse(el.date);
el.display_date = el.displaydate;
el.read_more_url = el.readmoreurl;
el.photo_url = el.photourl;
return el;
}
Btw. you do not need to use brackets on know property names without not standard names:
el['timestamp'] === el.timestamp
It will be easier if you paste your JSON