0

I have four dynamically inputs. User select how many wants. In input he enter article ID and on success I send this in URL. Url can have only two or three or four id

'http://someurl/' + ID + '/someParams'
example: 'http://someurl/991,992,993,994/someParams'

I pass ID's from html to controller

$scope.submitChoice = function (name) {
    $scope.choices.forEach(function(choice, index){
        if(choice.name)
        {
            switch (index) {
                case 0:
                    var firstId = choice.name;
                    break;
                case 1:
                    var secondId = choice.name;
                    break;
                case 2:
                    var thirdId = choice.name;
                    break;
                case 3:
                    var fourthId = choice.name;
                    break;
            }
            console.log(firstId, secondId, thirdId, fourthId);
            $scope.getBenchIdForCompare(firstId, secondId, thirdId,fourthId);
        }
    })
}    

I use SWITCH CASE and INDEX because I need every of this ID set to unique var (because of another part of my app). Problem is, when I submit entered ID in input, I get this in my console when I console.log(firstId, secondId, thirdId, fourthId);

console

991 undefined undefined undefined
undefined 992 undefined undefined
undefined undefined 993 undefined
undefined undefined undefined 994

And I can not pass this to another function because I need to have 991 992 993 994

Is there another way to make this?

2
  • 3
    Declare variables outside of for loop as well as move console.log and function $scope.getBenchIdForCompare call outside for loop. Commented Dec 4, 2017 at 13:22
  • 1
    @Arter, instead of variables take $scope.variable_names Commented Dec 4, 2017 at 13:37

2 Answers 2

2

You can use map instead and create an array. Once array is created, you can join them using join() method and you will get readymade comma separated string. This comma separated string can be added to get desired URL.

$scope.submitChoice = function(name) {
  var choices = $scope.choices.map(function(choice){
    return choice.name;
  });
  console.log(choices.join(','));
  $scope.getBenchIdForCompare(choices[0], choices[1], choices[2], choices[3]);
}

ES6 version (using the spread operator):

$scope.submitChoice = function(name) {
  let choices = $scope.choices.map(choice => choice.name)
  console.log(choices.join(','));
  $scope.getBenchIdForCompare(...choices);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I think you can even print it as a table with: console.table(choices)
Nice for ES6 👍
That's exactly what I need... Thank you, i learn something new :)
1

What's happening here is that you're calling the function on every single element of the choices array. Every element has a unique index. This causes the switch statement's case to be true only once for each element. But, since you're looping through the choices array, the other three variables never get assigned any value. Hence the undefineds.
I would suggest to move the declarations of the variables and their usage out of the forEach loop. Just use the forEach for assigning the values and then the code should work fine

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.