This question already has answers here:
How to check if an array contains another array?
(6 answers)
javascript search array of arrays
(12 answers)
How to compare arrays in JavaScript?
(55 answers)
Closed 3 years ago.
These codes don't work:
const a = [[1, 1], [2, 2]]
console.log(a.includes([1, 1])); // --> false
console.log(a.indexOf([1, 1])); // --> -1
This work but I think its not optimized
console.log(a.map(x => x.toString()).includes([1, 1].toString()));
// --> true
Is there a simpler way ?
const a = [[1, 1], [2, 2]]
var index=a.findIndex(x=>{return JSON.stringify(x)===JSON.stringify([2, 2])})
console.log(`item index : ${index}`);
Assuming this:
var arr = ['a', 'b', 'b'];
you can invoke:
Array.isArray(arr);
will return true if the considered variable is an array, otherwise not.
Once you get it, you can apply it to the external array.
Related
This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Filter Array modifying the original array itself [duplicate]
(3 answers)
Closed 3 months ago.
I'm trying yo filter the numeric values of an array with this code:
function getNumerics(toFilter) {
toFilter = toFilter.filter( element => !isNaN(element));
console.log(toFilter);
}
var toFilter = [1, 'z', '4', 2, 6];
getNumerics(toFilter);
console.log(toFilter);
The console.log inside the function shows a correct result but but the last console.log show the array with all the values but if I pass the array to the function why is not change? in javascript all parameters are passed are reference , aren't it?
Your function should return the filtered Array:
function getNumerics(toFilter) {
return toFilter.filter( element => !isNaN(element));
}
var toFilter = [1, 'z', '4', 2, 6];
toFilter = getNumerics(toFilter);
console.log(toFilter);
This question already has answers here:
Javascript equivalent of Python's zip function
(24 answers)
Zip arrays in JavaScript?
(5 answers)
Transposing a 2D-array in JavaScript
(25 answers)
Closed 1 year ago.
Hi guys I have this array :
[[2,3,0],[0,4,5]]
and i want traverse this array like:
[[2,0],
[3,4],
[0,5]]
Any recommendations please? I am working with javascript
You can easily achieve this result using the Array.prototype.map
const arr = [
[2, 3, 0],
[0, 4, 5],
];
const result = arr[0].map((val, i) => {
return Array(arr.length)
.fill("")
.map((_, index) => arr[index][i]);
});
console.log(result);
This question already has answers here:
How can I check if an object is an array? [duplicate]
(51 answers)
Closed 3 years ago.
How can I iterate over nested arrays like this with recursion in javascript:
var a = [10, [1, [2,2,2], 3], 20, 'Hallo']
In Python it looks like this:
def foo1(L):
for i in L:
if not isinstance(i, list):
print(i)
else:
foo1(i)
foo1(a)
How can I write if not isinstance(i, list): in javascript?
You can use:
var a = [10, [1, [2,2,2], 3], 20, 'Hallo']
for (let item of a) {
if (!Array.isArray(item)){
console.log("Not array", item);
}else{
console.log("Array", item);
}
}
This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 4 years ago.
I have an array, and I want an output if it contains more than 1 of the same element.
Example:
my_array = [1, 2, 3, 1];
if you want a Boolean output if an element is repeated you can do this:
var arr=[1,1,3,4]
let isDup=false;
arr.map(x=>(arr.indexOf(x)!==arr.lastIndexOf(x))?isDup=true:isDup)
console.log(isDup)
Convert the array to a Set. A Set can only contain unique values. If the Set's size is less than the array's length, there are duplicates:
const hasDuplicates = (arr) => arr.length > new Set(arr).size;
console.log(hasDuplicates([1, 2, 3])); // false
console.log(hasDuplicates([1, 2, 3, 1])); // true
This question already has answers here:
Summing ; delimited values in Javascript
(1 answer)
How to sum elements at the same index in array of arrays into a single array?
(7 answers)
Closed 5 years ago.
I am trying to add together an undetermined amount of arrays, but only add or reduce them item by item.
I cannot figure out how to make this work in ES6 javascript.
const arrayList = [
[1, 2, 3],
[1, 2, 3],
[2, 2, 2]
];
const totalRow = arrayList.map((array) => {
const total = [];
array.map((number, i) => {
total[i] = (total[i] == undefined ? 0 : total[i]) + number;
});
return total;
});
//Output is just 3 more arrays with no change, as it seems
//the total variable resets to an empty array with each iteration.
//what i want is this output:
[4, 6, 8]
What am I doing wrong? I tried reduce method as well but came up empty