Reputation: 60
I have created an associative array using following function.
function checkEmpty(){
var sizes = new Array();
var validate = new Array();
$j('div.option-fields').each(function(){
if($j(this).attr('id')) {
sizes.push($j(this).attr('id'));
}
});
for(var i=0; i< sizes.size(); i++){
$j("#"+sizes[i]+' input[type=text]').each(function(){
if($j(this).val()){
//validate.push(true);
validate[sizes[i]] = true;
}else{
//validate.push(false);
validate[sizes[i]] = false;
}
});
}
}
which return [size-278: true, size-287: true]
Now I want to search whether this result contain false in value or not, or whether all value with different indexes are same or not.
I used inArray but it is giving -1 every time.
Upvotes: 1
Views: 12592
Reputation: 11
To search for an a value within an associative array, you do it like so:
listOfLanguagesAndTheirSubtags = {"Afrikaans" : "AF", "Dansk" : "DA", "Deutsch": "DE", "English": "EN", "Italiano" : "IT" };
var values = Object.values(listOfLanguagesAndTheirSubtags);
var subtagToFind = "AF";
if(values.includes(subtagToFind))
{
console.log('value is found');
}
else
{
console.log('value NOT found');
}
To search whether all values with different indices are the same or not, you can go further like so:
var numberOfOccurencesOfSubtag = values.filter(function(value){
return value === subtagToFind;}).length
if(numberOfOccurencesOfSubtag === values.length)
{
console.log("All values with different indices are the same");//... as subtagToFind
}
else
{
console.log("All values with different indices are NOT the same");//... as subtagToFind
}
Upvotes: 1
Reputation: 665
It is better that validate
will be an Hash rather than Array
Search Associative Arrays here: https://www.w3schools.com/js/js_arrays.asp
so declare it like this:
var validate = {};
Than you will get something like this {size-278: true, size-287: true}
And you can look for false value like this:
var validate = {'size-278': true, 'size-287': true};
var values = Object.values(validate);
console.log(values.indexOf(false));
var validate = {'size-278': true, 'size-287': true};
var values = Object.values(validate);
console.log(values.indexOf(true));
Notice that Object.values
may not be supported in older browser
Upvotes: 2