We help IT Professionals succeed at work.

Javascript Array

aznprncritic
aznprncritic asked
on
I have the following array:

var queryItems = ["phone-selector-compared=prod1233,phone-selector-compared=,phone-selector-compared=,phone-selector-type=contract,phone-selector-category=phone-selector-blackberry,phone-selector-touchscreen=1,phone-selector-camera=0.01%2C100,phone-selector-styles=phone-style-default%2C+phone-style-flip%2C+phone-style-bar%2C+phone-style-slider"];

Open in new window


I'm using the following loop:

for(var i=0; i<queryItems.length; i++) {
			var queryItem = queryItems[i].split('=');
			$('[name=' + queryItem[0] + ']').filter( '[value=' + unescape(queryItem[1]) + ']' ).attr('checked', true);
		}

Open in new window


I need to:

1) determine if 'phone-selector-compared' equals an Id ('prod1233') and if it does, what is it?

Comment
Watch Question

leakim971Multitechnician
Distinguished Expert 2019

Commented:
>I need to : determine if 'phone-selector-compared' equals an Id ('prod1233') and if it does, what is it?

Please confirm this is what you're trying to do with the loop

Author

Commented:
Yes, lemme clarify:

The array will always contain "phone-selector-compared", I need to determine if "phone-selector-compared" will also have a value associated with it.  Does that make sense?
leakim971Multitechnician
Distinguished Expert 2019

Commented:
var queryItem = queryItems[i].split('=');
haveAssociatedValueWithHim = (queryItem[1] != null) && (queryItem[1].length>0);

Author

Commented:
Won't that just determine if any parameters in the array have values?

Also, if phone-selector-compared does have a parameter associated with it, i need to retrieve it.
Multitechnician
Distinguished Expert 2019
Commented:
>Won't that just determine if any parameters in the array have values?
No
var queryItems = ["phone-selector-compared=prod1233,phone-selector-compared=,phone-selector-compared=,phone-selector-type=contract,phone-selector-category=phone-selector-blackberry,phone-selector-touchscreen=1,phone-selector-camera=0.01%2C100,phone-selector-styles=phone-style-default%2C+phone-style-flip%2C+phone-style-bar%2C+phone-style-slider"];
for(var i=0; i<queryItems.length; i++) {
   var queryItem = queryItems[i].split('=');
   haveAssociatedValueWithHim = (queryItem[1] != null) && (queryItem[1].length>0);
   if(haveAssociatedValueWithHim) {
       // perhaps we need to work on this
       // $('[name=' + queryItem[0] + ']').filter( '[value=' + unescape(queryItem[1]) + ']' ).attr('checked', true);
       var theValue = $('[name=' + queryItem[1] + ']').val();
   }
}

Open in new window

leakim971Multitechnician
Distinguished Expert 2019

Commented:
Thanks for the points!