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

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);

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);

Removing duplicates in array in javaScript using hasOwnProperty [duplicate]

This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 5 months ago.
I have an array with duplicate elements. While trying to remove duplicate elements using hasOwnProperty getting one duplicate element in array rest of duplicate element removed successfully. expexted output = [1, 3, 2, 4, 5, 6, 7] but getting something [1, 3, 2, 3, 4, 5, 6, 7]. I can use different function and remove duplicates but I'm not understanding why element 3 is coming twice.
var array = [1,3,2,1,3,4,5,6,7,3,5,6,4,3]
let output = []
function removeDuplicates(array){
for(let item of array){
if(!output.hasOwnProperty(item))
output.push(item)
}
return output
}
console.log(removeDuplicates(array));
Instead of using hasOwnProperty, you can use includes.
var array = [1,3,2,1,3,4,5,6,7,3,5,6,4,3]
let output = []
function removeDuplicates(array){
for(let item of array){
if(!output.includes(item))
output.push(item)
}
return output
}
console.log(removeDuplicates(array));
hasOwnProperty checks whether an object contains a given key. The correct way to check if an array contains an element is to use includes:
if(!output.includes(item))
output.push(item)

Sort duplicate numbers from array in Javascript [duplicate]

This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 10 months ago.
I had got a question in my exam as follow:
we need to create function that will receive 1 array of all positive number and return all the duplicate values of the array in sorted order.
Here is the solution that I had implemented:
function solution(arr) {
return arr.filter((value,index)=>arr.indexOf(value)!==index).sort()
}
But the code was rejected. Can someone tell me what could be more optimized solution for this problem in Javascript?
maybe because of this :
solution(['sada','sada','r',4,'r','r']) => ['r', 'r', 'sada']
or
solution([2,2,1,3,3,6,7,7,7]) => [2, 3, 7, 7]
your solution when array have same thing more than 2 times, return wrong array .

Selecting a value in an array in an array; Javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
I have an array, that happens to be filled with arrays: [[1, "thing"], [2, "other thing"], [3, "still other thing"]] I want to be able to select a value inside one of the arrays without having to make the array a temporary variable and then pick the item. Can this be done?
You can index into a nested array with the same '[]' syntax.
var arr = [[1, "thing"], [2, "other thing"], [3, "still other thing"]];
console.log(arr[0]); // [1, "thing"]
console.log(arr[0][0]); // 1
For reference, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
In that link search for two-dimensional

Split an array in pieces [duplicate]

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.

Categories

Resources