Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm sure the title is very confusing but I'm not sure how to phrase it. I have a paper form that we are trying to put on an iPad via a webpage. The form consists of diagnoses and procedures. The workflow is that a user clicks on a diagnosis and then clicks on multiple procedures and then repeats the process.

I have an object that consists of the following properties to represent this workflow

  1. a string called diagnosis
  2. an array of strings called procedures
  3. a string called color

When a user clicks on a diagnosis and procedure it gives those items a background so that they can easily see that the procedure corresponds to the diagnosis. It is then added to an array of those objects. I'm working on the process of removing objects from the array if they click on the diagnosis or procedure again. I have the following code

if ( isDiagnosis || isProcedure )
    {
        if (isDiagnosis) {
            var item = $(this).html().trim();
            var result = arr.filter(function (obj) {
                return obj.diagnosis === item;
            })[0];
            alert(result.color);
        }
        if (isProcedure) {
            var item = $(this).html().trim();
            var result = arr.filter(function (obj) {
                return obj.procedure.indexOf(item) > -1;
            })[0];
            alert(result.color);
        }

    }

I'm alerting the color as part of the testing process. The code works correctly for diagnosis but I'm trying to figure out how to search for the object that has a procedure array that contains the value that is assigned to the variable item. I'm getting an error because obj is undefined and therefore has no method indexOf. Can someone please help me with the best way to search for the object that has the value of item in the procedure array property?

a sample json object (I know it's not in the right format, I'm not very good at json) if you click on the procedure 33333 then I want it to return the second object in the array.

    arr = [
    diagnoisset1 = {
        "diagnosis": "acne",
        "color": "red",
        "procedure": [ "11111", "22222" ]
    }, 
    diagnosisset2 = {
        "diagnosis": "adhd",
        "color": "green",
        "procedure": [ "33333", "44444" ]
    },
    diagnosisset3 = {
        "diagnosis": "broken leg",
        "color": "yellow",
        "procedure": [ "55555", "666666" ]
    };
]
share|improve this question
    
Is arr the described object, and if so, can you show some sample JSON for it? –  Two-Bit Alchemist Mar 16 at 1:40
    
What happens if you use an if statement to check if the obj is defined before you call something that won't work on undefined? –  Paul Mar 16 at 1:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.