knockout.js and sorted array on object - arrays

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.

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"])

Order collection of array objects

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?

AngularJS custom SortBy

I have an array with several objects {id:x , name:y} retrieved from a Java Enum and I need to order them alphabetically, but one of the objects must mandatorily be in the bottom of the <select> comboBox, how could I achieve this result? I used ng-options with orderBy, but, I can't figure out how to put this particular object into the bottom.
The easiest way is to sort in the controller, when you get the data from the server:
find the "special" object that should go to the bottom and remove it from the array
sort the array, using $filter('orderBy')(array, 'name')
push the "special" object to the sorted array
expose the sorted array on the scope and use that array in ng-options

How do I use .fill method to create new objects?

Specifically, I'm wondering why when I create a new array with several sub-arrays using .fill, the sub-arrays created using .fill are the same object.
array = Array.new.fill([], 0..8)
subsquare[0].object_id => 7220340
subsquare[1].object_id => 7220340
How would I create these sub-arrays while ensuring each sub-array was its own object?
The why to this question can be found if you look at Array.new:
http://ruby-doc.org/core-2.2.0/Array.html
Note that the second argument populates the array with references to
the same object. Therefore, it is only recommended in cases when you
need to instantiate arrays with natively immutable objects such as
Symbols, numbers, true or false.
To create an array with separate objects a block can be passed
instead. This method is safe to use with mutable objects such as
hashes, strings or other arrays
The workaround as specified in the doc is:
array = Array.new(8) { [] }

How to append to multidimensional array in a smarty template?

I can append to a single array using
{append var='name' value='Bob' index='first'}
However, if I have a multi-dimensional array such as:
$name[first][last] = ['this','array']
and I want to append another value to the array at $name[first][last] e.g. to make the array like this:
$name[first][last] = ['this','array','appended']
how can I do this in the smarty template?
You can do this without using append:
{$name[first][last][] = 'this'}
{$name[first][last][] = 'array'}
{$name[first][last][] = 'appended'}
I must highlight though - templates should be used for specific purpose: to display prepared data; having to do the above is a code smell
I've tested many cases to try achieve it and I think it's not possible (in documentation there is also no info or example of multidimensional key or var)
You should also really think do you need it at all. Logic should be in PHP and role of Smarty is only displaying data not manipulating them

Resources