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.
Related
I'm making a scheduling app, and storing all the scheduled things in firebase with arrays. When I try to schedule something with the same string value, it fails and doesn't add it to the array. I don't know if this is something in swift I can edit, or if it's a firebase setting.
If it's something in swift, here's the code updating the array:
doc.updateData([
"Instructor": FieldValue.arrayUnion(["\(scheduleinstructor)"])
])
If it's something in firebase, could someone please explain a way around this or a simple fix I overlooked?
According to the documentation on adding items to an array:
arrayUnion() adds elements to an array but only elements not already present
So the fact that the duplicate entry is not added is by design. If you want to allow that, you'll have to:
Read the document with the array from the databae.
Extract the array from the document into your application code.
Add the item to the array.
Write the entire modified array back to the database.
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.
![information is clearly there, but not being found][1]
Does anyone have any idea what I can do to my code to make this $.each / _.each work? (I'm using both jquery and underscore, either will do)
The Array[0] concerns me. Maybe that is why the .each is getting stepped over.
The two console.log's on either side of the each log the entire object perfectly, but when I break at the each and type "regionedAps" into the console, I get an empty array.
You have an array that you are treating like an object. You should make regionedAps an object, then .each will iterate over it properly.
var regionedAps = {};
I don't think you need $.makeArray()
this is the problematic:
I got a master array, it's an array that have all the entities in the game.
When an entity (unit, building, etc) dies, I remove that entity from the array.
Now, I got other several arrays that are "subgroups" from that array. Like, enemyEntities, alliedEntities, movingEntities, etc. Everytime I create a new entity I add it to the corresponding array.
Everything works ok, but, when I remove one element from the master array, I'd like to somehow automatically remove it from the other arrays, let say, in an elegant way.
Any ideas?
Have a remove-method which takes care of it all. Make it first remove it from the master array, and then from each sub arrays (if the object is found). A nicer way of doing it might be to put all arrays in a another array, and do that to each of them in a loop.
I have an Array to be displayed in a CakePHP View which contains the navigation data. Each array item contains fields like id, name, level etc. as well as another array field named children, where this structure repeats.
In the Cake PHP View it's easy to walk over the array on the first level with a foreach loop and the html-Helper to output the relevant data. But how do I ouput the child items (and then their children if present)? Normally I would do a recursive function call, but within the view you shouldn't use function look, should you? Bunt within the controller I cannot use the html helper, so I'm stuck here.
I think as a clean solution to make a helper to iterate over the array an recursively look for each elements if one of them another array (http://book.cakephp.org/#!/view/1097/Creating-Helpers)
Or if you know that your item could has one and only one child, you could ask if the actual item is an array an operate it. But I prefer the first solution.
It looks like you might try the tree behavior
http://book.cakephp.org/#!/view/1341/Basic-Usage
you need something like a recursive method that can call it's self, something like the following. https://github.com/infinitas/infinitas/blob/beta/core/menus/views/helpers/menu.php#L163