Array manipulation with nested arrays - arrays

let names = ['Ayomide',['Tolu',['Tola',['Chuka',['Dan']]]]];
How can I manipulate each element in this nested array. It's all index 0 and 1. Trying to figure out how to loop through this to get the elements individually, thanks

You could use name.flat(Infinity) to flatten it to a single array (Infinity tells it to go as deep as necessary), and then you can just use .forEach or whatever
let names = ['Ayomide',['Tolu',['Tola',['Chuka',['Dan']]]]];
console.log(names.flat(Infinity))

Related

How can I check if an array contain another array (ANGULAR)?

I'd like to know if there's a way to understand if an array contain another array inside him. I ask this question because I'm trying to loop throught two or plus nested arrays but first I need to check this in Angular.
Thanks in advance for the help
var a = [[1,2,3],[4,5,6]]
console.log(Array.isArray(a[0]))
You can check this using Array.isArray function.
There are many ways this can be accomplished, but it depends on the size of the parent array, how deeply nested it's elements are, how does much does performance matter, etc.
If your array only goes one level deep (inner arrays don't contain arrays), you can use these methods:
var arr = [[1,2,3], 'foo', 4, ['foo', 'bar']];
// use .some() to test elements
arr.some((element) => Array.isArray(element));
// returns the index of the first found array, or -1 if no arrays are present
arr.indexOf((element) => Array.isArray(element));
This article gives a pretty good overview of all options, including the performance of each.

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.

What is the best way to empty a Vector or Array in AS3?

What is the most efficient way to empty an Array or Vector in ActionScript 3?
I've always just re-initialized them:
vector = new Vector.<T>();
array = [];
It doesn't look like there's an empty() function or anything similar.
Is there a better way?
Re-initializing the array is fine in most cases, since the garbage collector will just sweep up the old array. Still, if you want to empty an array without creating a new one you can set array.length = 0
Another option is to use the splice method.
Array::splice documentation
For an array the following call empties it:
array.splice(0);
For a vector the second parameter is enforced, so the call becomes:
vector.splice(0, vector.length);

In CoffeeScript how do you append a value to an Array?

What is the prescribed way to append a value to an Array in CoffeeScript? I've checked the PragProg CoffeeScript book but it only discusses creating, slicing and splicing, and iterating, but not appending.
Good old push still works.
x = []
x.push 'a'
Far better is to use list comprehensions.
For instance rather than this:
things = []
for x in list
things.push x.color
do this instead:
things = (x.color for x in list)
If you are chaining calls then you want the append to return the array rather than it's length.
In this case you can use .concat([newElement])
Has to be [newElement] as concat is expecting an array like the one its concatenating to.
Not efficient but looks cool in the right setting.

Converting between an ActionScript Array (Object[]) and a Vector.<Object>

Are there options for converting an Array to a Vector in ActionScript without iterating the array?
What about the other way (converting a Vector to an Array)?
For the Array to Vector, use the Vector.<TYPE>() function that accept an array and will return the vector created :
var aObjects:Array = [{a:'1'}, {b:'2'}, {c:'3'}];
// use Vector function
var vObjects:Vector.<Object> = Vector.<Object>(aObjects);
For the other there is no builtin function, so you have to do a loop over each Vector item and put then into an Array
vObjects.push.apply(null, aObjects);
And another way to do it.
The trick here is simple. If you try to use the concat() method to load your array into a vector it will fail because the input is a vector and instead of adding the vector elements AS will add the whole vector as one entry. And if you were to use push() you'd have to go through all items in the array and add them one by one.
In ActionScript every function can be called in three ways:
Normal way: vObjects.push(aObjects)
Throws error because aObjects is not an Object but an Array.
The call method: vObjects.push.call(this, myObject1, myObject2, ..., myObjectN)
Doesn't help us because we cannot split the aObjects array into a comma-separated list that we can pass to the function.
The apply method: vObjects.push.apply(this, aObjects)
Going this route AS will happily accept the array as input and add its elements to the vector. You will still get a runtime error if the element types of the array and the vector do not match. Note the first parameter: it defines what is scoped to this when running the function, in most cases null will be fine, but if you use the this keyword in the function to call you should pass something other than null.
var myArray:Array = [].concat(myVector);
might work.

Resources