0

Sure it's a stupid mistake but can't see what I'm doing wrong here but when I test in my fiddle I can't get a result, could someone point out where I'm going wrong please?

In the code below and in the fiddle I'm trying to return the value for array id: 15.

https://jsfiddle.net/wc71ra6r/3/

Code

function findValueById(myA, fVal) {
    for(var i = 0; i < myA.length; i++){
        if(myA[i].id === fVal) {
            return myA[i].value;
        }
    }
}

var myArray = [{id: 10, value: 100},
               {id: 15, value: 300},
               {id: 20, value: 200];

alert(findValueById(myArray, 15)); // 300
2
  • 4
    You need to close the bracket for the last object in the array. Commented Jul 21, 2015 at 9:27
  • Add } to the last element in array. jsfiddle.net/tusharj/wc71ra6r/4 Commented Jul 21, 2015 at 9:28

3 Answers 3

3

You didn't close your array deceleration right: Replace your array declaration with this:

var myArray = [{id: 10, value: 100},
               {id: 15, value: 300},
               {id: 20, value: 200}];
Sign up to request clarification or add additional context in comments.

Comments

1
var myArray = [{id: 10, value: 100},
               {id: 15, value: 300},
               {id: 20, value: 200}];

Comments

0

Missing } in array

var myArray = [{id: 10, value: 100},
               {id: 15, value: 300},
               {id: 20, value: 200}];

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.