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})
});
Related
This question already has answers here:
How to filter an array from all elements of another array
(24 answers)
Closed 4 years ago.
I have this array
$scope.userEventData.selectedFlats and there's an other array $scope.flatsArray
I want to remove values from $scope.userEventData.selectedFlats that are present in $scope.flatsArray.
I have done this:
$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(function(f){
return !$scope.someObj.flatsArray.some(function(v){
return v.indexOf(f) >= 0;
})
})
But I get an error saying v.indexOf is not a function
The v in flatsArray.some callback function returns a single item rather than an array of items. So rather than checking for the index of, you can simply compare the values directly.
You need to
$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(function(f){
return !$scope.someObj.flatsArray.some(function(v){
return v == f;
})
})
Pick either some or indexOf. For example
$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(function(f){
return $scope.someObj.flatsArray.indexOf(f) === -1
})
or
$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(function(f){
return !$scope.someObj.flatsArray.some(function(item) { return item === f; })
})
It might be helpful to post the arrays so they can be used in answers, however, you could do this.
for (var i = 0; i < $scope.userEventData.selectedFlats; i++) {
var index = $scope.flatsArray.indexOf($scope.userEventData.selectedFlats[i]);
if ( index > -1 ) {
$scope.userEventData.selectedFlats.splice(index, 1);
}
}
This will loop through each item in the selectFlats array and find the index of that item in the flatsArray, then remove that item from if it exists.
Try this:
$scope.userEventData.selectedFlats = $scope.userEventData.selectedFlats.filter(
function(item) {
return !($scope.someObj.flatsArray.contains(item))
}
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))
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:
JS search in object values
(22 answers)
Closed 6 years ago.
I have an array of objects and I'd like to filter out objects based on matching values.
var a = [
{name:'xyz' , grade :'x'},
{name:'yaya' , grade :'x'},
{name:'x' , frade:'d'},
{name:'a',grade:'b'}
]
If I want to filter on 'x' then I'd expect the following results:
[
{name:'xyz' , grade :'x'},
{name:'yaya' , grade :'x'},
{name:'x' , frade:'d'},
]
I have tried this
a.filter(function(d) {
return d.name.toLowerCase().indexOf(searchKey.toLowerCase()) > -1 ||
d.grade.toLowerCase().indexOf(searchKey.toLowerCase()) > -1
});
but I don't want hardcoded keys.
function yourSearch (array, search) {
return array.filter((el) => {
for (let param in el) {
if (el.hasOwnProperty(param) && Array.isArray(el[param])) {
if (el[param].indexOf(search) !== -1) return true;
}
}
});
}
enjoy
if without es6 replace (el) => with function (el)
edit: added reply to comment
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.