This question already has answers here:
Display posts in descending posted order
(19 answers)
Closed 5 years ago.
I have an array in Firebase, which I'm getting like this:
var ref = new Firebase(url);
ref.orderByChild("date").on("child_added", function(obj) {
console.log(obj.val());
});
But I need that in descending order. How can I do that with Firebase?
Thanks!
Since you added the AngularJS tag, I'll assume you're using Angular in your project. In that case, it would make your life significantly easier to use AngularFire (http://angularfire.com).
Once you're using AngularFire, you can use the $firebaseArray service to grab your array from your database, and sort it just as you would any other array.
The $firebaseArray documentation can be found here: https://www.firebase.com/docs/web/libraries/angular/guide/synchronized-arrays.html
Related
This question already has answers here:
How can I update an object inside of an array in firestore?
(1 answer)
How to update an "array of objects" with Firestore?
(18 answers)
Is there any way to update a specific index from the array in Firestore
(3 answers)
Closed 8 months ago.
i am stuck with a problem with firebase (with Vue). I want to add the user id to a specific map (yes, maybe or no) when the user presses a specific button. But Im having trouble add/update the data because its nested.
Here is my data structure in Firestore
I want to add the user id in a map, something like this:
dates: {
[
0: {
yes: [userId1, userId2]
}
]
}
Does anyone can help me pushing the user ids into the arrays?
Unfortunately, right now it is not possible to update a specific element of the array. You can add new items or remove them - look here. But I would consider a little change in the approach and create a collection called dates in which you can define documents, which will contain yes, no, maybe arrays, which can be easily updated - in the way mentioned before.
This question already has answers here:
Getting JavaScript object key list
(19 answers)
Closed 1 year ago.
I am using the Alpha Vantage API to get stock market data. The data is returned in this format:
For charting purposes, I need to create an array or object of all of the dates. They are not within the actual data objects so I'm not sure how to do this.
For example, for this specific data I would want an array that looks something like this:
['2021-04-26', '2021-04-26', '2021-04-26', '2021-04-26', '2021-04-26', '2021-05-03'...]
Thanks for the help!
You can use Object.keys().
You didn't provide sample json, so this is formatted code and not a snippet.
const dates = Object.keys(data['Time Series (Daily)'])
This question already has answers here:
Dynamic object keys
(2 answers)
Closed 5 years ago.
I need some data to create graphs using Amcharts.
The chartData is the array containing all the data to represent in a graph.
Here is my code:
chartData6.push({
"Concession": $scope.concessionName,
"Total missions": $scope.nbMission,
$scope.brandName :$scope.nbMissionTotal
});
$scope.brandName is a dynamic value. I am not able to push it and my code won't even compile?
What should I do?
Just embed within the []
chartData6.push({
"Concession": $scope.concessionName,
"Total missions": $scope.nbMission,
[$scope.brandName] :$scope.nbMissionTotal
});
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 7 years ago.
I get an array of objects from the backend as this one, currently only three elements but in the future will be more.
[{"code":"lng.dropdown.home","text":"Inicio"},
{"code":"lng.dropdown.shop","text":"Tienda"},
{"code":"lng.button.support","text":"Soporte"}]
What is the most efficient way in javascript to find an element in this array by code, and return the text associated? Can we use lambdas expresions here?
you can use array filter to find the nested object(s) that matches your code property:
var arr =
[{"code":"lng.dropdown.home","text":"Inicio"},
{"code":"lng.dropdown.shop","text":"Tienda"},
{"code":"lng.button.support","text":"Soporte"}];
var res = arr.filter(function(x){
return x.code == 'lng.dropdown.home'
})
console.log(res[0])
[].filter returns an array with the objects that return true inside the callback.
As far as efficiency is concerned for loop are proved to be faster but using higher order methods you earn abstraction .
When not using other libraries, I usually map then find the indexOf, as follows:
data[data.map(function(a){ return a.code }).indexOf("code I am trying to find")];
This returns the element of data at the index where code matches the code you're trying to find.
When I have lodash as a dependency, I usually just do this however:
_.find(data, {code:"code I am trying to find"})
(I think that's the proper syntax)
Edit:
Didn't realize you were already using JQuery, the answer using $.grep is probably best.
This question already has answers here:
Trying to sort a custom JavaScript object
(5 answers)
Closed 8 years ago.
I know, I've found many examples to sort an Object. But that makes me a little bit confused (I am not so pro to check wich is the fastest and cleanest method for me).
I hope somebody can help.
My Object looks like
tt_results = {};
tt_results[6815631] = new Array('85', 'blibee', 'blaerb', 'bluo', 'tkone', 'tkeeee_seve', '382261471' );
tt_results[1563157] = new Array('25', 'blib', 'blab', 'bluerro', 'tkerone', 'tk_seve', '382266851' );
I want to sort the timestamp.
Thanks in advance!
Peter
As has been mentioned in the comment you cannot sort objects.
In terms of general js performance a good resource for x-browser tests & results check out jsperf. There is an array sort test here
See my answer to a similar question here: Trying to sort a custom JavaScript object
Basically, you can't sort objects themselves - it doesn't make sense. But you can process the object in sorted order by sorting the keys and then iterating over the sorted keys.