2

I have two sets of arrays, $scope.Skills and $scope.skillslist.

The Skills array has a ton of values, so I'm only including the ones relevant to the question:

$scope.Skills = [
    {LotsOfValues: "blah", Skill_Values__c: "1"},
    {LotsOfValues: "blah", Skill_Values__c: "2"},
    {LotsOfValues: "blah", Skill_Values__c: "3"}
];

And the skillslist array:

$scope.skillslist = [
    {Skill_Values__c: "1", Checked: ""},
    {Skill_Values__c: "2", Checked: ""},
    {Skill_Values__c: "3", Checked: ""},
    {Skill_Values__c: "4", Checked: ""},
    {Skill_Values__c: "5", Checked: ""},
    {Skill_Values__c: "6", Checked: ""},
    {Skill_Values__c: "7", Checked: ""}
];

What I want to do is, for each item in the skillslist, run it against the items in Skills, and if there's a match, set Checked to true, if not set it to false. How can this be accomplished?

I'm using the forEach function and this is what I have so far:

angular.forEach(skills, function (value) {
    console.log("FOR EACH");
    for (var i = 0; i <= $scope.skillslist.length; i++) {
        if ($scope.skillslist[i].Skill_Values__c === value.Skill_Values__c) {
            $scope.skillslist[i].Checked = "Added";
        } else {
            $scope.skillslist[i].Checked = "no";
        }
    }
});

But straight away I know I'm doing it wrong because I should be looking forEach skillslist right? But when I do that I have no idea how to then change the Checked value for each one. Swapping all Skills for skillslist didn't help.

In my ng-repeat of skillslist, I see the first item has a Checked value of "Added", the rest have a Checked value of "no".

I've been reading through forEach examples, but couldn't find any related to what I'm trying to do, about referencing two different arrays in this way. Any pointers would be appreciated!

2
  • Style suggestions: variable names generally shouldn't be capitalized and don't use sentinel values like "Added" and "no" use the built in boolean values. Commented Nov 25, 2015 at 20:33
  • Thanks for that, will keep lowercase in mind next time! I'm only using "added" and "no" as they related to css classes that get applied for the ng-repeat items, I didn't really want class names called true/false Commented Nov 25, 2015 at 21:04

2 Answers 2

3

If you're iterating over skillslist, each value passed to your callback will be the individual values of skillslist. So if you want to change the values of skillslist, you can do so by changing the first argument passed to your callback. Like this:

skillslist.forEach(function(skill) {
  var isInSkills = skills.some(function(skillValue) {
    return skillValue.Skill_Values__c === skill.Skill_Values__c;
  });
  if(isInSkills) {
    skill.checked = "Added";
  } else {
    skill.checked = "no";
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that SimpleJ, that's really helpful. I didn't realise I could approach it like that at all for comparing two arrays.
0

You can look at JavaScript's some and forEach:

$scope.skillslist.forEach(function (skillFromList) {
    skillFromList.Checked = $scope.Skills.some(function (skill) {
        return skillFromList.Skill_Values__c === skill.Skill_Values__c;
    });
});

JSFiddle

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.