Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list like

_Value1 = "'apple','ball','cat'....so on";

If I know that apple exists in above list.How to get index of whole string from the list.Like apple should have the index 1, ball should have 2 and so on.

What is the javascript code for this stuff?
Posted

Hope this may help you out...

JavaScript
var str = "a,b,c,d,e,f,g";

function getIndexOf(srcString, searchString) {
	var str2Array = srcString.split(',');
	var array2Index = [];
	for(var i = 0; i < str2Array.length; i++) {
		array2Index[str2Array[i]] = i;
	}
	return (typeof array2Index[searchString] != 'undefined' ? array2Index[searchString] : -1);
}

alert(getIndexOf(str,'a'));
alert(getIndexOf(str,'d'));
alert(getIndexOf(str,'g'));
alert(getIndexOf(str,'not found'));


Thanks & Regards,
Niral Soni
 
Share this answer
 
Comments
123arvind123 26-Aug-11 0:58am    
wow!!! it works thanks niral...
Do you mean indexOf?
e.g.
JavaScript
var array = ["foo","boo", "goo"];
var booIndex = array.indexOf("boo")


Note IE (at least IE7) doesn't support it.
 
Share this answer
 
v2
Comments
123arvind123 26-Aug-11 0:59am    
thanks for your reply but I am not using array...well I will go with what Niral suggest..but I appreciate your reply..
CPallini 26-Aug-11 2:50am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900