Split an array in pieces [duplicate] - javascript

This question already has answers here:
Split array into chunks
(73 answers)
Partitioning in JavaScript [duplicate]
(7 answers)
Closed 9 years ago.
I am new in CoffeeScript (I started learning this morning) and the only thing that I am not able to find is an elegant way to split an array in subarrays. For example, I have the following array:
myarr = [1, 7, 8, 3, 5, 1, 4, 9, 0, 2]
And I want to split it in subarrays of two elements each one:
myarr = [[1, 7], [8, 3], [5, 1], [4, 9], [0, 2]]
I know how to do it with pure javascript however, and given I am learning, I was not able to find an elegant-coffeescript way. Does CoffeeScript allow a nice solution for this?
Update:
using the http://js2coffee.org/ site to translate my javascript code to CoffeScript I was able to find this solution:
Array::chunk = (chunkSize) ->
array = this
[].concat.apply [], #map((elem, i) ->
(if i % chunkSize then [] else [array.slice(i, i + chunkSize)])
)
It looks quite right. But that is a translator output and not a expert recommendation.
Disclaimer:
Even when CoffeeScript compiles to Javascript, I am looking for a CoffeeScript solution and not a Javascript solution. The idea that "if you know javascript then, you know coffeescript" is not true for me, at least not yet.

Related

comparing elements in an array in JS [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed last month.
How can I compare elements in an array like if I for example have arr=[1, 2, 3, 4, 4] and I wanna find out if there are any duplicates and remove them.Are there any fuctions that I can use for this?
you can use a set data structure inbuilt into JS
let arr=[1, 2, 3, 4, 4] ;
let output = [...new Set(arr)]
You need to convert the array to a set, A set has unique elements only as opposed to arrays that can have duplicates. To convert to a set
let arr=[1, 2, 3, 4, 4]
let set = new Set(arr);

How to Clone an Array With a Removed Element? [duplicate]

This question already has answers here:
How to get subarray from array?
(5 answers)
Closed 2 years ago.
I've searched up this question, and everywhere people seem to recommend to use array.splice(). However, splice is inplace, and, for example, in my javascript console editor.
Everywhere I seem to search, people say that splice does NOT mutate the original array, but that is clearly not the case. Now, I'm sure I will find another way to do what I want, but what is the proper way to make a copy of a piece of an array without affecting the original array?
You can use slice(), see below:
let x = [1, 2, 3, 4, 5]
console.log(x);
let sliced = x.slice(0, 2);
console.log(x);
console.log(sliced);
The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included) where begin and end represent the index of items in that array. The original array will not be modified.
Make a copy of the array using the spread operator and then you can use splice or whatever.
let arr = [1, 2, 3, 4, 5];
let newArr = [...arr];
console.log(newArr);
// newArr.splice(......)

It seems that array sorting method is not working properly [duplicate]

This question already has answers here:
How to sort an array of integers correctly
(32 answers)
Closed 3 years ago.
Array.sort() function isn't returning expected result. Is that how it actualy works, or what?
const arr = [1, 5, 12, 8, 17];
console.log(arr.sort());
Expected result would be: [1, 5, 8, 12, 17]
but result I get is: [1, 12, 17, 5, 8]
You need to pass a callback to sort array of integers. If you don't pass a callback by default it will sort according to UTF-16 code value units. According to MDN
The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values
const arr = [1, 5, 12, 8, 17];
console.log(arr.sort((a,b) => a - b));

What does a and b stand for in the sorting function [duplicate]

This question already has answers here:
How does Javascript's sort() work?
(8 answers)
Closed 4 years ago.
I have started learning javascript a few days ago and I have a bad time understand what these lines:
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
What is a and b equal to in this function?
Thank you very much for your help in advance
The sorting algorithm implement by sort() will call the specified function with various combinations of elements from array to determine which element should go first in the order. The two chosen element for comparison will be a and b.

Is an array of arrays supported in Javascript? [duplicate]

This question already has answers here:
How can I create a two dimensional array in JavaScript?
(56 answers)
Closed 6 years ago.
I was under the impression that an array of arrays is not allowed in javascript. Is that still true? Is the following object valid?
object: { array: [ [0],
['1', '2']
]
}
Can someone please point me to examples of usage? I've been using arrays of strings as a workaround.
It is allowed. Try this.
var arrayObj = [
[1, 2],
[3, 4],
[5, 6]
];
console.log(arrayObj[0][0]); // first column/first row
console.log(arrayObj);

Categories

Resources