I am trying to iterate through an array (or tuple, etc.) and want to remove certain items if they meet my criteria. The output could a modified array or a new array altogether if I can't modify the existing object I am iterating through.
for item in simple_array
if some_condition
add_to_new_array(item) # or remove from my existing array
end
end
You could use filter , or if you want to modify the existing array use filter! instead.
filter(isodd, simple_array)
Related
I am trying to reach to the 3rd cn Array (highlighted) in, please suggest and logic and sample code.
To solve the nested array, first see the schema of the preceding array, keep the notice of the elements and then follow the following logic to get the array exploded.
explode(preceding_array.first_lv_element.nested_array_name)
I used the below code
cn_cn_cn_arr=cn_cn_arr.select(cn_cn_arr.pid,
cn_cn_arr.id,
explode(cn_cn_arr.cn_cn.cn).alias('cn_cn_cn'))
(1) In my Canvas App I create a collection like this:
Collect(colShoppingBasket; {Category: varCategoryTitle ; Supplier: lblSupplier.Text ; ProductNumber: lblProductNumber.Text });;
It works - I get a collection. And whenever I push my "Add to shopping basket" button, an item are added to my collection.
(2) Now I want to sort the collection and then use the sorted output for other things.
This function sorts it by supplier. No problems here:
Sort(colShoppingBasket; Supplier)
(3) Then I want to display the SORTED version of the collection in various scenarios. And this is where the issue is. Because all I can do is manipulate a DISPLAY of the collection "colShoppingBasket" - (unsorted).
(4) What would be really nice would be the option to create and store a manipulated copy of the original collection. And the display that whereever I needed. Sort of:
Collect(colShoppingBasketSORTED; { Sort(colShoppingBasket; supplier) });; <--- p.s. I know this is not a working function
You could use the following:
ClearCollect(colShoppingBasketSorted, Sort(colShoppingBasket, Supplier))
Note that it is without the { }
This will Clear and Collect the whole colShoppingBasket sorted.
If you want to store the table in a single row in a collection, you can use
ClearCollect(colShoppingBasketSortedAlternative, {SingleRow: Sort(colShoppingBasket, Supplier)})
I wouldn't recommend this though because if you want to use the Sorted values you'd have to do something like this:
First(colShoppingBasketSortedAlternative).SingleRow -> this returns the first records of the colShoppingBasketSortedAlternative then gets the content of the SingleRow column, which in this case is a Collection
Note: You will need to replace the , with ; to work on your case
I have an angular app with cloud firestore with the following structure:
--nombre
--grupo
--nombre1
--nombre2
--nombre3
--numerocuenta1
--numerocuenta2
--numerocuenta3
--resuelto
--0: false
--1: false
.
.
.
--13: false
As you can see, at the end I have an array of boolean (resuelto) which contains 13 elements, I want to change a value of a certain index when an element with the same index is clicked
¿Is there a way to access each index of that array and change its value?
I tried with this:
this.taskDoc = this.afs.doc(`tasks/${task`);
this.taskDoc.update(resuelto[0]); <--
And this:
this.taskDoc.update(resuelto.0); <--
Firestore doesn't support update of individual array elements. If you have an array and want to change one of its value, you'll have to read the entire array, change the value locally, then write the entire array back.
It might be better for you to model your data as an object rather than an array. Object properties may be individually updated by calling out the object field with the dot notation.
While you cannot do operations on certain elements in an array by index, you can add or remove elements in an array by value using arrayUnion and arrayRemove:
doc.update("someField" : FieldValue.arrayUnion("someValue"))
doc.update("someField" : FieldValue.arrayRemove("someValue"))
This makes arrays act more like a set then an array but it at least gives you some options. more documentation here: https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html
I need to modify a field value for all records in a crossfilter before inserting new records.
The API doesn't say anything about it. Is there a way to do that ?
Even if it's a hack that would be really useful to me.
Looking at the code, the data array is held as a private local variable inside the crossfilter function so there's no way to get at it directly.
With that said, it looks like Crossfilter really tries to minimize the number of copies of the data it makes. So callback functions like the ones passed into crossfilter.dimension or dimension.filter are passed the actual records themselves from the data array (using the native Array.map) so any changes to make to the records will be made to the main records.
With that said, you obviously need to be very careful that you're not changing anything that is relied on by the existing dimensions, filters or groups. Otherwise you'll end up with data that doesn't agree with the internal Crossfilter structures and chaos will ensue.
The cool thing about .remove is it only removes entries that match the currently applied filters. So if you create a 'unique dimension' that returns a unique value for every entry in your dataset (like an ID column), you can have a function like this:
function editEntry(id, changes) {
uniqueDimension.filter(id); // filter to the item you want to change
var selectedEntry = uniqueDimension.top(1)[0]; // get the item
_.extend(selectedEntry, changes); // apply changes to it
ndx.remove(); // remove all items that pass the current filter (which will just be the item we are changing
ndx.add([selectedEntry]); // re-add the item
uniqueDimension.filter(null); // clear the filter
dc.redrawAll(); // redraw the UI
}
At the least you could do a cf.remove() then readd the adjusted data with cf.add().
I wanted to add element in array dynamically.Size is not fixed.Shall I use arraylist or array?Datatype is same.I am adding the value from database whose type is int.Then which datatype shall I used?How to add and remove a value from array or arraylist?
Use Generic List instead of ArrayList to avoid overhead of type casting.
See sample