Order collection of array objects - arrays

After making some queryes i have a collection of array objects. But after orderByDesc the data transform in hole objects, instead of how it was (array of objects), what it happened? Is like the method orderByDesc tranform the collection in another type.
Ex:
some logic code, queries...etc
in the end
return $total->sortByDesc('date');
After using sortByDesc:
Before to sortByDesc:
Now that is in type of objects is giving some issues working with it in my javascript application (angularjs), it not a array of objects how should it be.

Try to copy the collection when sorting and use values()
$sorted = $total->sortByDesc('date');
dd($sorted->values());
Same result?

Related

How to access object values using object["key"] approach

In the code I am referring, object attributes are accessed using the object["key"] method instead of calling object.key to access attribute values.
But when I try to create a simple object array and access attributes using above approach, I am getting below error.
if bank_record.effective_date.strip() == "25/07/2019" and bank_record["description"].__contains__("50036"):
TypeError: 'COM' object is not subscriptable
The reason given for object is not subscriptable error is missing __getitem__ method for the class. But in the code I am referring, it doesn't contain such method for any of the dto classes. But the above object["key"] method works just fine. What am I missing. I have been trying to figure this out for a while.
I just want to loop through a object array and access object attributes and modify them on the run. In order to make the function generic, I want to access these object attributes using object["key"] approach. Please help..
My mistake, I have missed set of steps. In the code I am referring, they are looping a json object array, which is created by dumping, python object array values into a json string and loaded back to a json object array.
excel_dto_list = []
#add objects to the list
#...
json_string = json.dumps([ob.__dict__ for ob in excel_dto_list])
#done in another method
downloaded_object = json.loads(json_string)
for x in downloaded_object:
print(x["comment"])

How to iterate through an array with objects and array of objects in multiple levels?

I have an array which contains objects. These objects may have other arrays of objects, then those objects also may have other objects and array of objects. There is no limit really.
So how do I go through all the objects in this array no matter how deeply nested they are? If I know how deep the arrays go, I can easily do this by using a number of for loops. I having problem with going through all the elements.
I am not able to come up with a logic to do that. Can somebody please help me? I am trying to do this in swift.
Have an Any object.
Store the values into that object how you need it.
When you read the object, cast it to the required array.
If the casting is successfull, then you can access that Any object like an array.

Storing array data in firebase, and how ID's are generated

I have a set of objects in my firebase data that all have an array under them. When I create the initial object, I create the initial array with its first object with a line of code like this:
ref.child('items').set([{firstobject: id123}])
this seems to set the id to zero, as the first item in the array. However when I later try to push() a new item to the array with this line of code, I get a more complex id (ZwPiVMIrzbSdvfwxkts).
ref.child('items').push(someNewObject);
In your first line of code, you're calling the Firebase.set() method passing it a JavaScript array that contains a single object.
In your second line of code, you're calling the Firebase.push() method with an object.
Given that Firebase lists/collections are not the same as JavaScript arrays, you end up with a mismatch.
Unlike JavaScript arrays, Firebase's lists are architected to scale well in highly concurrent, multi-user scenarios. I'd recommend to use them instead of arrays from the start.
ref.child('items').push({firstobject: id123});
ref.child('items').push(someNewObject);
With this snippet, all your items will be stored under so-called push ids.

replace collection in a view

I want to replace a collection inside a view. I use the reset command like this:
var maColl=mContent.get(ici).get("svgParameterList");
msvgParameterListView.collection.reset(maColl);
A JSON.stringify gives this :
maColl
[{"id":"x","name":"x"},....{"id":"style","name":"style"}]
msvgParameterListView.collection
[[{"id":"x","name":"x"},....{"id":"style","name":"style"}]]
Now, i have my collection in an array, so when i render it return null value.
How to change array of collection into collection ?
In short, how to make msvgParameterListView.collection equal to maColl ?
Note the [[ ]] for the new collection.
Note maColl is a collection inside another collection.
If I understand correctly, you are trying to reset a collection with the models of another collection? collection.toJSON is your friend:
toJSON collection.toJSON()
Return an array containing the attributes hash of each model in the collection. This can be used to
serialize and persist the collection as a whole. The name of this
method is a bit confusing, because it conforms to JavaScript's JSON
API.
which could be applied like this
msvgParameterListView.collection.reset(maColl.toJSON());
Or if you want to keep a reference to the original models, pass maColl.models
msvgParameterListView.collection.reset(maColl.models);
Passing directly a collection to reset will only confuse Backbone.

knockout.js and sorted array on object

Can I do a foreach on a function that takes an array object and sorts it. I know I can do a dependent observable, but I have an array of an array coming from the mapping plugin.
foreach: sortList($array)
Yes... Here's a fiddle to demonstrate:
http://jsfiddle.net/jearles/sHQvV/
It shows three usages of foreach:
1) Plain
2) Function returning array
3) Object with data property containing an array
The foreach documentation describes this.

Resources