0

How can I get the sum these 2 arrays in JS or jquery. I also want to check if b exists or not empty. Here's my code:

$('select[name=changer]').change(function() {
  var one = $(this).val();
  if (one == 'one') {
    alert('One');
  } else {
    var all_grade1 = $('select[name=grade\\[\\]]').val() || [];
    var all_grade2 = $('select[name=has_grade\\[\\]]').val() || [];
    var alls = all_grade1.concat(all_grade2);

    var yes = alls.reduce(getSum, 0);
    alert(yes);
  }

});

function getSum(total, value) {
  return total + parseInt(value, 10);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="changer">
  <option value="one">One</option>
  <option value="multiple">Multiple</option>
</select>
<hr />
<select name="grade[]">
  <option value="1">Grade1</option>
  <option value="2">Grade2</option>
  <option value="3">Grade3</option>
</select>
<select name="grade[]">
  <option value="2">Grade2</option>
  <option value="4">Grade4</option>
  <option value="6">Grade6</option>
</select>
<select name="has_grade[]">
  <option value="1">HasGrade1</option>
  <option value="2">HasGrade2</option>
  <option value="3">HasGrade3</option>
</select>

I have research about this, but nothing seems to be match with my problem. Any suggestions/answers?

3
  • show the values of your two arrays and what output you are expecting based on those two array Commented Jun 10, 2017 at 7:24
  • Do you want to get the sum of lengths of both array or its values? Commented Jun 10, 2017 at 7:26
  • If alls was an array of numbers your code should work.I doubt that jquery gets an array directly with val(), you probably have either arrays with strings or worse a comma delimited string.Just console.log your values and check. Commented Jun 10, 2017 at 7:37

1 Answer 1

2

Query the DOM for all the relevant <select> elements

$('select[name=grade\\[\\]], select[name=has_grade\\[\\]]')

Iterate over them and get the value of the selected options as numbers

.get().map(function(select) {
    return parseInt(select.value, 10)
})

and sum them up

.reduce(getSum, 0)

All together this would look something like this:

$('select[name=changer]').change(function() {
  var one = $(this).val();

  if (one == 'one') {
    console.log('One');
  } else {
    var selects = $('select[name=grade\\[\\]], select[name=has_grade\\[\\]]');
    var yes = selects.get()                    // get the matched DOM nodes as an array
                      .map(function(select) {  // get the values (as numbers) of the selected options
                        return parseInt(select.value, 10);
                      })
                      .reduce(getSum, 0);      // sum the selected options

    console.log(yes);
  }
});

$('select[name=changer]').change(function() {
  var one = $(this).val();

  if (one == 'one') {
    console.log('One');
  } else {
    var selects = $('select[name=grade\\[\\]], select[name=has_grade\\[\\]]');
    var yes = selects.get()                    // get the matched DOM nodes as an array
                      .map(function(select) {  // get the values (as numbers) of the selected options
                        return parseInt(select.value, 10);
                      })
                      .reduce(getSum, 0);      // sum the selected options

    console.log(yes);
  }
});

function getSum(total, value) {
  return total + parseInt(value, 10); // or parseFloat() depending on the value
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="changer">
  <option value="one">One</option>
  <option value="multiple">Multiple</option>
</select>
<hr />
<select name="grade[]">
  <option value="1">Grade1</option>
  <option value="2">Grade2</option>
  <option value="3">Grade3</option>
</select>

<select name="grade[]">
  <option value="2">Grade2</option>
  <option value="4">Grade4</option>
  <option value="6">Grade6</option>
</select>
<select name="has_grade[]">
  <option value="1">HasGrade1</option>
  <option value="2">HasGrade2</option>
  <option value="3">HasGrade3</option>
</select>

Sign up to request clarification or add additional context in comments.

5 Comments

it returns an error: TypeError: alls.reduce is not a function
@Jonjie In this snippet? If so, what browser do you use? .reduce() is available since IE9. If it is in your own code, than please show it (jsfiddle.net, jsbin.com, pastebin.com, ...)
Im using firefox. Have you tried 2 exam[] and/or 2 has_exam[]?
Please see this JSFiddle
Looks nice, I'll do it later sir.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.