This question already has answers here:
Why is using "for...in" for array iteration a bad idea?
(28 answers)
Closed 4 years ago.
What is the most efficient way to check a certain condition on every element of an array and return false if one or more elements do not meet the condition, for example, I have this array for example
arr = ["foo","azeaze", "wazeazerar"]
for(var ar in arr){
if(ar.length > 5){
console.log(false)
}else{
console.log(true)
}
}
as you can see it return true even if the element "foo" length is not greater than 5
You can use Array.prototype.every() in a one line function
arr = ["foo","azeaze", "wazeazerar"]
const isittrue = currentval => currentval.length > 2
console.log(arr.every(isittrue));
arr = ["foo","azeaze", "wazeazerar"]
console.log(arr.every(elem => elem.length >= 5))
Related
This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
In Javascript, how do I check if an array has duplicate values?
(9 answers)
Checking for duplicate strings in JavaScript array
(13 answers)
Closed 5 months ago.
I am writing a javascript function that takes a nested array and returns the numbers that occurs more than once in that array.
I believe my function is accurate (meaning that it passes their "Correctness test" ) but i am after efficiency, how efficient is this code?
For example - Lets call the name of the function deepSort(nestedArray) where nestedArray is the nested array parameter
function deepSort(nestedArray) {
const flatArr = nestedArray.flat().sort();
let results = []
for (let i = 0; i < flatArr.length - 1; i++) {
if (flatArr[i + 1] == flatArr[i]) {
results.push(flatArr[i]);
}
}
return (results.filter((item, index) => results.indexOf(item) === index)).join()
}
const a = deepSort([[1,3,4,5], [4,7,9,1,3], [2,3,5], [1,2,3,4]]) // Returns 1,2,3,4,5
console.log(a);
const b = deepSort([[1,2,3], [4,5], [6,7,8], [2,9,0]]) // Returns 2
console.log(b);
const c = deepSort([[2,7,9], [4,3], [9,6,5], [1,4,3]]) // Returns 3,4,9
console.log(c);
Can this code be optimized any more for speed and efficiency when handling extremely large values of data?
This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 12 months ago.
How can i check if an array has element twice and log only the element that is not.
const duplicateElements = (array) => {
for(let numbers of array) {
// code here
}
}
const numbers = [1,3,2,4,1,3,2];
duplicateElements(numbers);
// Output 4
With the JS function filter you can archive this. First you have to iterate your array. then check with the filter function how many times the current value is inside your array. If equal 1 then push to an result array.
const d = [];
const arr = [1,3,2,4,1,3,2]
arr.forEach((e) => {
if (arr.filter(x => x == e).length === 1) {
d.push(e);
}
})
console.log(d);
This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Closed 5 years ago.
Suppose I have an array: var a=[[1,1],[2,2],[3,3],[4,4]] If I write a[1] it returns [2,2]. But if I want to return the index of the array element [2,2] like a.indexOf([2,2]) it returns -1 which is not found. Is there an elegant way to find the index of an array element in an array?
You can use Array.prototype.findIndex()
var index = a.findIndex(function(el) {
return el[0] == 2 && el[1] === 2
});
var index = a.findIndex(function(el) {
return el.every(function(n) {return n === 2})
});
This question already has answers here:
Check if all values of array are equal
(33 answers)
Closed 6 years ago.
I have an array for example
var a = [1,4,6,1,1,1,1];
and I need to compare each element in array for similarity. If all of them are similar I need return true, if one or more of them are different it should return false
Will be glad to get the answer.
Here's one method to achieve it, by using Set.
var a = [1,1,1,1];
var b = [1,2,3,4,5,1,2,3];
function check(arr) {
console.log([...new Set(arr)].length == 1 ? true : false);
}
check(a);
check(b);
if they all need to be the same then you could just check to see if everything in the array is equal to the first element by using filter and length. The length of the array filtered by any element in the list should equal the original length.
const a = [1, 4, 1, 1, 1, 1, 1];
function similarity(arr) {
let firstItem = arr[0];
return arr.filter(elements => elements == firstItem).length != arr.length ? false : true;
}
console.log(similarity(a));
You can make use of the every Method.
From MDN
The every() method tests whether all elements in the array pass the
test implemented by the provided function.
var notsimilar= [1,4,6,1,1,1,1];
var similar= [2,2,2];
console.log(notsimilar.every((x,i,a) => a[i] === a[0]));
console.log(similar.every((x,i,a) => a[i] === a[0]));
This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 8 years ago.
I have an object as follows -
var users = {
room: [1,2,3,4]
}
How do I find if the 3 exists in the room array ?
JS bin
Use indexOf:
var indexOfThree = users.room.indexOf(3);
if(indexOfThree != -1)
{
var three = users.room[indexOfThree];
}
else
{
console.log("not found");
}
it will return -1 if the element isn't found or else it's index in the array.