0

There is a string that is being formulated something like this Item:ShortName:Tax and if more items are added, it is appended to string like Item:ShortName:Tax_Item2:ShortName2:Tax2, adding one more, Item:ShortName:Tax_Item2:ShortName2:Tax2_Item3:ShortName3:Tax3 and so on. What I want to know is that, I have to check for if a item is present in the list, say, shirt, or jeans. It would be the first in array when separated by colon.

I used this.

var list = "Item:ShortName:Tax_Item2:ShortName2:Tax2_Item3:ShortName3:Tax3_Item4:ShortName4:Tax4";

$(document).ready(function () {
    var arg = 'Item2';
    findItem(arg);
    fucntion findItem(argument) {
        $.each(list.split('_'), function () {
            if (this.split(':')[0] == arg) {
                alert('Item found!');
            }
        };
        };

But the alert doesn't appear, though Item2 is in the list.
In case its not clear, this list split by _ contains a array (as you can see) and each of that array separated by : contains Itemname, ItemShortName, TaxOnItem, now in all those small array, I have to check for first array that contains the ItemName to see if a particular item exists or not! How can I do this?

3 Answers 3

1

You do realise you got an error in your code, fucntion findItem(argument) { } think you mean "function".

Sign up to request clarification or add additional context in comments.

Comments

0

You are missing some { and ( as well as a typo. The console log tells you all this!

$(document).ready(function () {

    var list = "Item:ShortName:Tax_Item2:ShortName2:Tax2_Item3:ShortName3:Tax3_Item4:ShortName4:Tax4";
    var arg = 'Item2';
    findItem(arg);

    function findItem(argument) {
        $.each(list.split('_'), function () {
            if (this.split(':')[0] == arg) {
                alert('Item found!');
            }
        });
}

}​);​

Comments

0

Here: I slightly reformatted restructured your code. Try this:

var list = "Item:ShortName:Tax_Item2:ShortName2:Tax2_Item3:ShortName3:Tax3_Item4:ShortName4:Tax4";

function findItem(argument) {
    list.split("_").forEach(function(subList){
        if(subList.split(":")[0] == argument) { 
             alert("Item found!");
         }
    });
};

$(document).ready(function () {
    findItem("Item2");
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.