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.
Related
This question already has answers here:
Get the index of the object inside an array, matching a condition
(16 answers)
Closed 2 years ago.
I have an array of object that holds two objects. Each object has a couple properties; one being dealerNo. Given the value of dealerNo, how would I get the index of that object?
Use .findIndex:
const getIndexByDealerNo = (arr=[], dealerNo) =>
arr.findIndex(e => e.dealerNo===dealerNo);
const arr = [ { dealerNo:1 }, { dealerNo: 2 } ];
console.log( getIndexByDealerNo(arr, 1) );
console.log( getIndexByDealerNo(arr, 2) );
console.log( getIndexByDealerNo(arr, 11) );
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:
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:
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:
Length of a JavaScript object
(43 answers)
Closed 9 years ago.
I have an Object. How to get number of nested objects in a?
a = {
firstNested: {
name: 'George'
}
secondNested: {
name: 'James'
}
}
I supposed to use .length which is usually used for arrays. What should I do in this case?
Yes, duplicate of above.. Just use:
var a = {1:2, 3:4},
count = 0,
item;
for(item in a) {
count++;
}
alert(count);