Home javascript foreach Adjustment of array values ​​(JS)

foreach Adjustment of array values ​​(JS)

Author

Date

Category

Tell me whether there is a more competent solution for the addition of array values
Now I do this

count = [];
Array.Foreach (Data, Index) {
Count [index] = data.number;
});
Eval (Count.Join ('+'));

displays the amount (here all the rules), but since I am new to JS, maybe there is a more competent and more correct decision, and not glue everything through Eval.

tried different options, but something I could not achieve a positive effect.


Answer 1, Authority 100%

var count = 0;
Array.Foreach (FUNCTION (VALUE, INDEX) {
  Count + = + Value;
});

Answer 2, Authority 400%

Use reduce :

var sum = array.Reduce (FUNCTION (PREVIOUSVALUE, VALUE) {
  RETURN PREVIOUSVALUE + VALUE;
});

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions