This question already has answers here:
Firestore Update single item in an array field
(2 answers)
How can I update an object inside of an array in firestore?
(1 answer)
Closed 6 months ago.
I'm trying to make a button that can edit an item within a documents array in firebase. Basically, the functionality I'm building is: you click an EDIT button, and it changes a tag into an tag where you would make your edit/update to the array item. Then you would click a DONE button and the update to the array would happen. For some reason, I can only find documentation on firebase for updating a document, and not an item inside the array. I've tried using arrayUnion but it only adds a new item to the array. I'm trying to update the info[0].tags in the screenshot below. Please let me know if I'm missing information to help make this work.
My issue seems to be I can't do: info[i]: arrayUnion({tags: tags}) so I'm not sure how to tell the updateDoc function which item in the info array I need to update.
Here's the code:
const editCard = async (tags, i) => {
const fieldRef = doc(db, "labels", activeLabels);
await updateDoc(fieldRef, {
info[i]: arrayUnion({ tags: tags }),
});
console.log(tags);
getLabels();
findLabel(activeLabels);
toast.success("Tags Updated!");
};
[![firebase document and array][1]][1]
[1]: https://i.stack.imgur.com/PDJcc.png
This question already has answers here:
Convert [key1,val1,key2,val2] to a dict?
(12 answers)
Make dictionary from list with python [duplicate]
(5 answers)
Convert list into a dictionary [duplicate]
(4 answers)
Closed 4 years ago.
trying to figure out how to do this and have yet to find a good solution. I pulled this data out of an XML response. It was in a var tag. Now what I would like to do is create a dictionary out of it. The domain.com should be paired with the number right listed behind it.
This is the data:
[
'cb131.domain1.com', '147827',
'cb143.domain2.com', '147825',
'cb175.domain1.com', '147454',
'cb190.domain.com', '146210',
'cb201.domain.com', '146208',
'cb219.domain.com', '146042',
'cb225.domain.com', '146282',
'cb900.domain.com', '148461',
'cb901.domain.com', '148493',
'cb902.domain.com', '148495',
'cb903.domain.com', '148497',
'cb904.domain.com','148499',
'cb905.domain.com', '148501',
'cb906.domain.com', '148503',
'cb907.domain.com', '148505',
'cb908.domain.com', '148507',
'cb909.domain.com', '148509'
]
So for example cb131.domain1.com should be paired with 147827, cb143.domain2.com paired with 147825 and so on.
Drawing a blank on a good quick solution on how to do this. Hopefully someone can help.
Thanks!
Edited with answer I choose below:
I choose this answer and also to help anyone else I add a nice way to print out the results (data is the string I obtained):
import ast
i = iter(ast.literal_eval(data))
dic = dict(zip(i, i))
for key , value in dic.items():
print(key, " :: ", value)
This should do it. Assuming the list is saved to a variable l:
keys = l[::2]
vals = l[1::2]
dic = dict(zip(keys, vals))
You can create an iterator from the list after using ast.literal_eval to parse it from the input text, zip the iterator with itself, and pass the generated sequence of tuples to the dict constructor:
import ast
i = iter(ast.literal_eval(data))
dict(zip(i, i))
Assuming you have the above in a python array called data, you can do:
new_data = []
for i in range(0, len(data), 2):
new_data.append((data[i], data[i+1]))
Now new_data would be a list of tuples. You could certainly create a better data structure to hold these pairs if you want.
I do not yet know Python that I can write a snippet, but:
initialize an empty dictionary in Python
create a for loop counting index from 0 to length of your array in steps of two.
inside add a dictionary entry with key of value at index and value at index + 1
perhaps check for duplicates
Does this answer help you?
This is Python - quickly google'd:
dictionary = { }
for idx in range(0, len(data), 2)
dictionary[data[idx]] = data[idx + 1]
This question already has answers here:
Firebase search by child value
(2 answers)
Closed 4 years ago.
basically I new to firebase, I just want to that how to retrive specific data from firebase ,
[firabase](https://i.stack.imgur.com/vk0S6.png[1]
as you can see in above image I want to retrieve like in Sql we use
Select CellNum from Student where Email="Adnan#gmail.com.
How can I do that in firebase web javascript?
Try the following:
firebase.database().ref().child("Students").orderByChild("Email").equalTo("Adnan#gmail.com").once("value", function (snapshot) {
snapshot.forEach(function(childSnapshot) {
var cellNum=childSnapshot.val().CellNum;
});
});
The snapshot is at Students, then you loop inside the id 22222 and retrieve the CellNum. The orderByChild is the query where Email="Adnan#gmail.com"
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
Probably very easy question, but I couldn't find solution.
How to get data from this object?
That how it looks in consolo.log()
UPDATE:
Thank you for you answers.
That what I used before and it worked, but when I try on this array it returns error.
console.log(array[1].data);
Output picture
UPDATE2:
So I tried to make it a text, but I couldn't.
console.log(tempArray);
console.log("String: " + tempArray.toString());
console.log("Stringify: " + JSON.stringify(tempArray));
Here is output:
Stringify attempt result
Maybe there is something wrong with how I create this array.
let tempArray = [];
And in the loop
tempArray.push({"id": id, "data": data.routes[0].geometry});
Thank you,
Dmitry
That's an array of objects, so you would get elements from it like so:
console.log(obj[i].data)
Where i is the element (numbered 0 through 2) that you want to access.
This question already has answers here:
parsing out ajax json results
(3 answers)
Closed 9 years ago.
I'm very, very new to APIs (an hour in), and I'm just trying to get to the point where I can output a single part of an API response into console.log - and work from there.
Here's the working code which grabs all the data (for example, to display the last price in Bitcoin:
$.ajax({
url: "https://api.bitcoinaverage.com/ticker/all",
dataType: 'json',
success: function(results){
var gpbvalue = results;
console.log(gpbvalue);
}
});
And here's the data itself: https://api.bitcoinaverage.com/ticker/all
How would I specify just the 'last' value under GPB, rather than outputting the entire set of data?
Thank you so much for any help!
This is what you want.
console.log(results.GBP.last);
var gbpvalue = results.GBP.last;
or results["GBP"]["last"], both are equivalent.
The response is a JSON where the first key is a country code and the last value is a key under that. If you wanted to pick a specific last value you could access it like this:
console.log(results['AUD']['last']);
Or if you wanted all last keys you could do this:
for(key in results) {
console.log(results[key]['last']);
}
You can use dot notation but one of the keys 24h_avg is an invalid variable name (vars can't start with numbers) so named index notation is a better habit to get into.