I need to join multiple arrays together as one array. I can make an array with both arrays as its contents, but thats not what I need. Anyone know?
Hi
If you mean adding one of the contents to the end of another array, Iād do something like: (also I write this in python sorry)
def arrayCombine(x, y):
result = []
for i in x:
result.append(i)
for i in y:
result.append(i)
return result
Iām doing this away from the editor, I cannot guarantee the example will work in a literal sense, but Iād do that in some shape or form
Hi!
I just wanted to share a different approach to this! In python, you can actually just use the plus operator to join two lists together!
So, if you have list x and y, you can get the result list by doing: result = x + y
!
It can be nice to find solutions where loops are not necessary.
Yeah thats about my current solution but in blocks form, using for loops through every value of each array. Gets quite big when joining all 9 arrays- But thanks!
never seen that before⦠just popped up now
i might be able to help i have something like that that a friend shared. give me a week or so to find it and potentially modify it/test it if it even is relevant and useful to this
Hehehe I can watch you type
oh right! I forgot this was a feasble solution; I like doing loops like this, idk why (i never make my code readable lol), but efficiency seems best here
Ah I didnāt know that, thanks so much!
Of course!
Iām not sure of the greatest blocks solution for this, but will update you if I find something helpful!
seeing as moderation is in place (delaying time between post and view), why does this feature (actually seeing whos typing in real time) actually exist?
Just a feature of the platform. Even if you might not see the responding post right away, it might also be nice to know that someone is at least looking at your post and that you will have an answer soon.
oh cool
see it in slomo debug mode (evil laugh)
I know itās kind of late, but JavaScript has a convenient Array.prototype.concat()
method. It returns a value without modifying the original array.
Given array1 = [1, 2] & array2 = [3, 4]
:
let array3 = array1.concat(array2);
āarray3ā will be defined as [1, 2, 3, 4]
(this method is stackable, so you can do array1.concat(array2).concat(array3)...
In blocks you need to write your own method, as one is not in place. You could implement it something like this (pseudocode) :
function add_arrays(array1, array2)
define new_array = array1
for item in array do
new_array.push(item)
return new_array
Or alternatively (blocks) :
Ah thanks! Iām much more fluent in JavaScript than Python so Iāll probably use that- Thanks for showing me!!
If you wanted to make a function that takes in any number of arrays to be concatenated, you could make use of the previously shown custom method to use it in a loop.
(items is an array of arrays which are to be concatenated)
function add_many(items)
define new_array = []
if items.length <= 1 do
return items
for array in items do
new_array = add_arrays(new_array, array)
return new_array
In this method, I continuously update the ānew_arrayā variable by concatenating the next array to it.
And the blocks implementation:
Now, the blocks are complaining about type annotations - because TS really likes types (who could have known). The thing is that you canāt define types in blocks, so I kinda have no idea how to get around this. Even if I manually add them in the JS editor, the changes revert when I go back to blocks. The only other option I see at this point is if I just make a quick extension (if I can find some time for it) to concatenate which handles all the code and logic in JS anyways. For now your best bet is to just use my first method as many times as you have arrays (inconvenient but itās the best we have rn)ā¦
@richard, is there a way to circumvent this in blocks, or am I stuck?
yeah, unfortunately this does not work in blocks because of how our type inference works. generic types do not have the best support right now. can you give me a link to your code, though @Sarge? i would love to have it as a test case if we ever go improve this.
Of course! Hereās the code above
So, about thatā¦
Yeah, I did it anyway