0

I need to loop through the data array using the following function and return 'True' in case the function finds an object with particular values. However, when I fire the function it returns the following error message:

Cannot read property 'isTrue' of undefined

Could you please take a look and explain what I am doing wrong?

var data = [
 {
 category: "1",
 values: [
 { key: "valueToGet1", isTrue:"true" },
 { key: "valueToGet2", isTrue:"false" }
 ]
 },
 {
 category: "2",
 values: [
 { key: "valueToGet3", isTrue:"false"},
 { key: "valueToGet4", isTrue:"true"}
 ]
 }
 ]
var getValue = function (value) {
 var result;
 $.each(data, function (i, item) {
 result = $.grep(item.values, function (e) {
 return e.key == value;
 })[0];
 })
 return result.isTrue == 'true';
}
 
console.log(getValue('valueToGet4')); //should return true;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Ibrahim Khan
20.8k7 gold badges46 silver badges57 bronze badges
asked Dec 22, 2016 at 12:19
2
  • refer the link stackoverflow.com/questions/41197554/… Commented Dec 22, 2016 at 12:20
  • The error said that result is undefined, so some problem you have with data object I guess. Did you try to pass the whole array as parameter? getValue('valueToGet4', data) and the definition: var getValue = function(value, array) Commented Dec 22, 2016 at 12:26

2 Answers 2

4

You can use .some()

var getValue = function(value) {
 //Iterate each element in the array
 return data.some(d => {
 //iterate values property of the array item
 return d.values.some(v => {
 return v.key == value && v.isTrue == "true";
 })
 });
}

var data = [{
 category: "1",
 values: [{
 key: "valueToGet1",
 isTrue: "true"
 }, {
 key: "valueToGet2",
 isTrue: "false"
 }]
}, {
 category: "2",
 values: [{
 key: "valueToGet3",
 isTrue: "false"
 }, {
 key: "valueToGet4",
 isTrue: "true"
 }]
}]
var getValue = function(value) {
 return data.some(d => {
 return d.values.some(v => {
 return v.key == value && v.isTrue == "true"
 })
 });
}
console.log(getValue('valueToGet4')); //should return true;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

answered Dec 22, 2016 at 12:31
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, most appropriate one!
1

Use Reduce to exclude the items you don't want:

var getValue = function (value) {
 var result = data.filter(function (item) {
 result = item.values.filter(function (e) {
 return e.key == value && e.isTrue == 'true';
 });
 return result.length > 0 
 })
 return result.length > 0 ;
} 
answered Dec 22, 2016 at 12:34

1 Comment

thanks for another solution, will definitely consider using this

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.