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
3 Answers 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
var myArray = [{id: 10, value: 100},
{id: 15, value: 300},
{id: 20, value: 200}];
Comments
Missing } in array
var myArray = [{id: 10, value: 100},
{id: 15, value: 300},
{id: 20, value: 200}];
answered Jul 21, 2015 at 9:30
Vishnu Prasad
7271 gold badge11 silver badges28 bronze badges
Comments
lang-js
}to the last element in array. jsfiddle.net/tusharj/wc71ra6r/4