Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can _lodash test an array to check if an array element has a field with a certain value?

I have a variable selectedSubTopicId and I have an array of subTopic objects: objectiveDetail.subTopics[]. Each subTopic object has a field subTopicId

I would like to use this to enable or disable and Add topic button. Can I use lodash in the ng-disabled to test this array and report true if any subTopic object element of the array has a subTopicId that is equal to the selectedSubTopicId.

Here's a sample of the data that's in objectiveDetail. In this case there's just one element in the subTopics array.

{"objectiveDetailId":285,  "objectiveId":29,  "number":1,  "text":"x",  "subTopics":[{"subTopicId":1,                "number":1}] } 

Here is the code in my Angular Controller suggested by thefourtheye:

    $scope.checkDuplicateSubTopicId = function (objectiveDetail, sSubTopic) {         if (_.some(objectiveDetail.subTopics, function(currentTopic) {             return _.contains(currentTopic, selectedSubTopicId);         })) {             return true;         } else {             return false;         }     } 

My button with the click function not shown looks like this:

   <button data-ng-disabled="checkDuplicateSubTopicId(objectiveDetail, subTopicId)">        Add Topic    </button> 

The problem is that it's not quite working and the button does not show disabled.

like image 633
Samantha J T Star Avatar asked Jan 22 '14 09:01

Samantha J T Star


People also ask

How do you check if an array contains a specific value?

JavaScript Array includes() The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.

How do you check if an array contains a value from another array?

Use the inbuilt ES6 function some() to iterate through each and every element of first array and to test the array. Use the inbuilt function includes() with second array to check if element exist in the first array or not. If element exist then return true else return false.

How do you check if an array has a specific value in JavaScript?

For primitive values, use the array. includes() method to check if an array contains a value. For objects, use the isEqual() helper function to compare objects and array. some() method to check if the array contains the object.


1 Answers

You didn't ask for how to do it, but I assume that's what you wanted to know.

As I already mentioned, you can use _.some, which will iterate over every element in the array and execute a callback. In that callback you can test whether the value of the topic's property equals the value of the variable:

var result = _.some(objectiveDetail.subTopics, function (topic) {   return topic.subTopicId === selectedSubTopicId; }); 

_.some will skip the remaining elements if it found one for which the callback returned true.

like image 57
Felix Kling Avatar answered Sep 23 '22 09:09

Felix Kling