Pass current object first Array as3 - arrays

Is it possible to pass an array whilst placing the currentTarget object first in the array?
I've searched online and only found reference to index, indexOf etc but no resources on how to do this anywhere.
I can send the array no problem but it's always as first loaded, through selecting a different object it must be possible to ammend the array or splice the new object to the beginning but would really be grateful of any assistance as to how to achieve this.

Not 100% sure what you're asking, but you can use the Array::unShift(...args) method to insert an object at the beginning of an array.
Edit:
From your comment below, I think I get what you're saying. You can just swap the item you're looking for with the one at the beginning:
var index = array.indexOf(event.currentTarget);
array[index] = array[0];
array[0] = event.currentTarget;
or something similar to that anyways.

Related

How to add an array to Firebase Firestore Swift

I am trying to update the data in a document in Cloud Firestore with an array however whenever I try to do this I get an error: Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Unsupported type: __SwiftValue (found in field upvotes)'. I would really appreciate it if someone could help me understand what I am doing wrong and how I can fix this. Thanks. Code is below.
Code for updating array-
db.collection("forum").document(data[0].id!).setData(["upvotes": data[0].upvotes!.remove(at: data[0].upvotes!.firstIndex(of: MainView.username ?? "Anonymous")!)])
You must pass an array to Firestore to update an array property. What you're doing is passing an element of an array. The remove(at:) method, when used like this:
let removedElement = someArray.remove(at: someIndex)
will remove the element but return the value of that removed element; it doesn't return the updated array. You must first remove the element and then get the truncated array:
someArray.remove(at: someIndex)
let updated = someArray
Therefore, first remove, then update:
data[0].upvotes!.remove(at: data[0].upvotes!.firstIndex(of: MainView.username ?? "Anonymous")! // first remove
db.collection("forum").document(data[0].id!).setData([
"upvotes": data[0].upvotes!) // then pass
])
However, I'd recommend reformatting the code because it doesn't read very well, IMO, and there is too much force unwrapping for my taste.

Access Element in Paginated Dictionary. Dictionary within a list. Python 3.6

I am trying to access and cache (set) the value element in a paginated dictionary. I want each new paginated dictionary to do this. There is a way to go through the dictionaries with "before" and "after" parameters, but I think that might be an unnecessary tidbit.
When I do this:
[{x:1,y:2}]
pag_dict = foo.get_dict()
xvalue = bar.set(pag_dict["x"])
print(xvalue)
I get "TypeError: list indices must be integers or slices, not str" for the line with xvalue = bar.set(pag_dict["x"]). I just need it to print out 1 by accessing x.
I keep running into problems every way I try to fix this. Help would be extremely appreciated.
Thank you!
Based on discussion in comment:
pag_dict = [[{'x': 1, 'y': 2}]]
The above shows pag_dict is a list of list with 1 element. The element is of type Python's dict, which is a key-value pairs.
To get value of key x, try this:
# this gives "1"
pag_dict[0][0]['x']

Removing a specific element from an array by name in AS3

I would like to remove a specific element from an array, not by index because the index value of that item is not static.
myarray.splice(myclip, 1);
When I use this code instead flash removes the first element in the array.
Is there something I am missing here?
Documentation of splice()
Both parameters need to be integers, the first one is the position of the element you want to delete, and the second one is the amount of elements you want to delete. Try myarray.splice(myarray.indexOf(myclip),1);
Don't know why it would only remove the first element in your snippet, maybe internally it casts myclip to 0? Doesn't matter, use indexOf. If that doesn't work, for loop through the array to get the position first.

How to derive values from an array using the index value

I am trying to use an array in NetLogo to store my images and call the values using their index. Looks like I am getting stuck with common manner of accessing the array's value via arrayName[0].
How do I do that in NetLogo? googling doesn't seem to have the answer.
My array:
let imgArray ["easy1.png" "easy2.png" "easy3.png" "easy4.png" "easy5.png"]
I am trying to fix the image in the following manner:
clear-drawing import-drawing imgArray[1]
You're looking for item:
item 1 imgArray
Note that it's zero-indexed, so the first item is item 0 imgArray, though first imgArray is more idiomatic.
Also, arrays in NetLogo are called lists.

Array won't refresh itself after removeChild

There is an array of objects. I'm trying to removeChild an object from that array like below. removeChild works fine but the array won't refresh itself after removing uppest object. As you can see in below, i tried to trace array items out.
Firstly, array has three items, obviously the myArray.length must be 3.
After removing a child, myArray.length must be 2, but it get 3 (Wrong).
removeChild(myArray[currShape]);
trace(myArray);
Please tell me what am i missing here.
Assuming you're using ActionScript, removeChild() only serves to take objects off the stage. It doesn't take things out of an array. You have to take the object out of the array manually in another statement.
You could try something like:
removeChild(myArray.splice(currShape,1));
This removes the entry from the array and returns that entry that will be used to remove it from the stage.

Resources