substract an element of an array after multiply two elements on the same array












0














I'm getting info from an api, and i multiply two elements from the response, and then, sum the result. This is my code so far:



function forTest(){
$http.get('/api/DaApi?di=' + '2018-11-01' + '&df=' + '2018-11-01')
.then(function(data){
$scope.result = data.data.Response;

$scope.multiplyResult = $scope.result.map(x => x.Cost * x.Quantity).reduce((a, b) => a + b, 0);
}


Everything works perfect, but now i have to make another operation before the total sum. Let's say now i have 3 elements: Cost, Quantity and Discount:



[{Quantity: 2, Cost: 1000, Discount: -100},
{Quantity: 3, Cost: 2000, Discount: -500},
{Quantity: 2, Costo: 3130, Discount: -120}]


And now i need to multiply Quantity per Cost, and substract the Discount(which is already a negative number). After multiply and substract, i have to sum all the results. How can i make the substraction? Inside of map i have to make the subsraction? Something like:



$scope.multiplyResult = $scope.result.map(x => (x.Cost * x.Quantity) + x.Discount)).reduce((a, b) => a + b, 0);


Or must be inside of the reduce?



Someone can help me please, i'm a little far to understand the use of map and reduce. I'm using javascript and AngularJs.



Thanx in advance.










share|improve this question




















  • 2




    Should work fine in the map() as shown. Are you getting unexpected results?
    – charlietfl
    Nov 12 '18 at 22:26










  • Did you run the code? What errors or issues did you encounter? This doesn't seem to have anything to do with Angular other than how you're getting the data. Note that x.Discount is possibly a string, so you'll want (...) + +x.Discount to coerce it to number first, otherwise you'll get concatenation.
    – RobG
    Nov 12 '18 at 22:28












  • Maybe you want Quantity * (Cost + Discount).
    – georgeawg
    Nov 12 '18 at 22:55


















0














I'm getting info from an api, and i multiply two elements from the response, and then, sum the result. This is my code so far:



function forTest(){
$http.get('/api/DaApi?di=' + '2018-11-01' + '&df=' + '2018-11-01')
.then(function(data){
$scope.result = data.data.Response;

$scope.multiplyResult = $scope.result.map(x => x.Cost * x.Quantity).reduce((a, b) => a + b, 0);
}


Everything works perfect, but now i have to make another operation before the total sum. Let's say now i have 3 elements: Cost, Quantity and Discount:



[{Quantity: 2, Cost: 1000, Discount: -100},
{Quantity: 3, Cost: 2000, Discount: -500},
{Quantity: 2, Costo: 3130, Discount: -120}]


And now i need to multiply Quantity per Cost, and substract the Discount(which is already a negative number). After multiply and substract, i have to sum all the results. How can i make the substraction? Inside of map i have to make the subsraction? Something like:



$scope.multiplyResult = $scope.result.map(x => (x.Cost * x.Quantity) + x.Discount)).reduce((a, b) => a + b, 0);


Or must be inside of the reduce?



Someone can help me please, i'm a little far to understand the use of map and reduce. I'm using javascript and AngularJs.



Thanx in advance.










share|improve this question




















  • 2




    Should work fine in the map() as shown. Are you getting unexpected results?
    – charlietfl
    Nov 12 '18 at 22:26










  • Did you run the code? What errors or issues did you encounter? This doesn't seem to have anything to do with Angular other than how you're getting the data. Note that x.Discount is possibly a string, so you'll want (...) + +x.Discount to coerce it to number first, otherwise you'll get concatenation.
    – RobG
    Nov 12 '18 at 22:28












  • Maybe you want Quantity * (Cost + Discount).
    – georgeawg
    Nov 12 '18 at 22:55
















0












0








0







I'm getting info from an api, and i multiply two elements from the response, and then, sum the result. This is my code so far:



function forTest(){
$http.get('/api/DaApi?di=' + '2018-11-01' + '&df=' + '2018-11-01')
.then(function(data){
$scope.result = data.data.Response;

$scope.multiplyResult = $scope.result.map(x => x.Cost * x.Quantity).reduce((a, b) => a + b, 0);
}


Everything works perfect, but now i have to make another operation before the total sum. Let's say now i have 3 elements: Cost, Quantity and Discount:



[{Quantity: 2, Cost: 1000, Discount: -100},
{Quantity: 3, Cost: 2000, Discount: -500},
{Quantity: 2, Costo: 3130, Discount: -120}]


And now i need to multiply Quantity per Cost, and substract the Discount(which is already a negative number). After multiply and substract, i have to sum all the results. How can i make the substraction? Inside of map i have to make the subsraction? Something like:



$scope.multiplyResult = $scope.result.map(x => (x.Cost * x.Quantity) + x.Discount)).reduce((a, b) => a + b, 0);


Or must be inside of the reduce?



Someone can help me please, i'm a little far to understand the use of map and reduce. I'm using javascript and AngularJs.



Thanx in advance.










share|improve this question















I'm getting info from an api, and i multiply two elements from the response, and then, sum the result. This is my code so far:



function forTest(){
$http.get('/api/DaApi?di=' + '2018-11-01' + '&df=' + '2018-11-01')
.then(function(data){
$scope.result = data.data.Response;

$scope.multiplyResult = $scope.result.map(x => x.Cost * x.Quantity).reduce((a, b) => a + b, 0);
}


Everything works perfect, but now i have to make another operation before the total sum. Let's say now i have 3 elements: Cost, Quantity and Discount:



[{Quantity: 2, Cost: 1000, Discount: -100},
{Quantity: 3, Cost: 2000, Discount: -500},
{Quantity: 2, Costo: 3130, Discount: -120}]


And now i need to multiply Quantity per Cost, and substract the Discount(which is already a negative number). After multiply and substract, i have to sum all the results. How can i make the substraction? Inside of map i have to make the subsraction? Something like:



$scope.multiplyResult = $scope.result.map(x => (x.Cost * x.Quantity) + x.Discount)).reduce((a, b) => a + b, 0);


Or must be inside of the reduce?



Someone can help me please, i'm a little far to understand the use of map and reduce. I'm using javascript and AngularJs.



Thanx in advance.







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 22:50









georgeawg

32.9k104967




32.9k104967










asked Nov 12 '18 at 22:23









Chuck VillavicencioChuck Villavicencio

13110




13110








  • 2




    Should work fine in the map() as shown. Are you getting unexpected results?
    – charlietfl
    Nov 12 '18 at 22:26










  • Did you run the code? What errors or issues did you encounter? This doesn't seem to have anything to do with Angular other than how you're getting the data. Note that x.Discount is possibly a string, so you'll want (...) + +x.Discount to coerce it to number first, otherwise you'll get concatenation.
    – RobG
    Nov 12 '18 at 22:28












  • Maybe you want Quantity * (Cost + Discount).
    – georgeawg
    Nov 12 '18 at 22:55
















  • 2




    Should work fine in the map() as shown. Are you getting unexpected results?
    – charlietfl
    Nov 12 '18 at 22:26










  • Did you run the code? What errors or issues did you encounter? This doesn't seem to have anything to do with Angular other than how you're getting the data. Note that x.Discount is possibly a string, so you'll want (...) + +x.Discount to coerce it to number first, otherwise you'll get concatenation.
    – RobG
    Nov 12 '18 at 22:28












  • Maybe you want Quantity * (Cost + Discount).
    – georgeawg
    Nov 12 '18 at 22:55










2




2




Should work fine in the map() as shown. Are you getting unexpected results?
– charlietfl
Nov 12 '18 at 22:26




Should work fine in the map() as shown. Are you getting unexpected results?
– charlietfl
Nov 12 '18 at 22:26












Did you run the code? What errors or issues did you encounter? This doesn't seem to have anything to do with Angular other than how you're getting the data. Note that x.Discount is possibly a string, so you'll want (...) + +x.Discount to coerce it to number first, otherwise you'll get concatenation.
– RobG
Nov 12 '18 at 22:28






Did you run the code? What errors or issues did you encounter? This doesn't seem to have anything to do with Angular other than how you're getting the data. Note that x.Discount is possibly a string, so you'll want (...) + +x.Discount to coerce it to number first, otherwise you'll get concatenation.
– RobG
Nov 12 '18 at 22:28














Maybe you want Quantity * (Cost + Discount).
– georgeawg
Nov 12 '18 at 22:55






Maybe you want Quantity * (Cost + Discount).
– georgeawg
Nov 12 '18 at 22:55














2 Answers
2






active

oldest

votes


















1














I think it was a typo.






let arr = [{Quantity: 2, Cost: 1000, Discount: -100},
{Quantity: 3, Cost: 2000, Discount: -500},
{Quantity: 2, Cost: 3130, Discount: -120}];

let sum = arr.map(x => (x.Cost * x.Quantity) + x.Discount).reduce((a,b)=>a+b,0)
console.log(sum);








share|improve this answer





























    0














    You could calculate and sum in a single loop by taking a destructuring assignment for the needed properties and then add to sum the price.



    $scope.multiplyResult = $scope.result
    .reduce((sum, { Cost, Quantity, Discount }) => sum + Cost * Quantity + Discount, 0);





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270965%2fsubstract-an-element-of-an-array-after-multiply-two-elements-on-the-same-array%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      I think it was a typo.






      let arr = [{Quantity: 2, Cost: 1000, Discount: -100},
      {Quantity: 3, Cost: 2000, Discount: -500},
      {Quantity: 2, Cost: 3130, Discount: -120}];

      let sum = arr.map(x => (x.Cost * x.Quantity) + x.Discount).reduce((a,b)=>a+b,0)
      console.log(sum);








      share|improve this answer


























        1














        I think it was a typo.






        let arr = [{Quantity: 2, Cost: 1000, Discount: -100},
        {Quantity: 3, Cost: 2000, Discount: -500},
        {Quantity: 2, Cost: 3130, Discount: -120}];

        let sum = arr.map(x => (x.Cost * x.Quantity) + x.Discount).reduce((a,b)=>a+b,0)
        console.log(sum);








        share|improve this answer
























          1












          1








          1






          I think it was a typo.






          let arr = [{Quantity: 2, Cost: 1000, Discount: -100},
          {Quantity: 3, Cost: 2000, Discount: -500},
          {Quantity: 2, Cost: 3130, Discount: -120}];

          let sum = arr.map(x => (x.Cost * x.Quantity) + x.Discount).reduce((a,b)=>a+b,0)
          console.log(sum);








          share|improve this answer












          I think it was a typo.






          let arr = [{Quantity: 2, Cost: 1000, Discount: -100},
          {Quantity: 3, Cost: 2000, Discount: -500},
          {Quantity: 2, Cost: 3130, Discount: -120}];

          let sum = arr.map(x => (x.Cost * x.Quantity) + x.Discount).reduce((a,b)=>a+b,0)
          console.log(sum);








          let arr = [{Quantity: 2, Cost: 1000, Discount: -100},
          {Quantity: 3, Cost: 2000, Discount: -500},
          {Quantity: 2, Cost: 3130, Discount: -120}];

          let sum = arr.map(x => (x.Cost * x.Quantity) + x.Discount).reduce((a,b)=>a+b,0)
          console.log(sum);





          let arr = [{Quantity: 2, Cost: 1000, Discount: -100},
          {Quantity: 3, Cost: 2000, Discount: -500},
          {Quantity: 2, Cost: 3130, Discount: -120}];

          let sum = arr.map(x => (x.Cost * x.Quantity) + x.Discount).reduce((a,b)=>a+b,0)
          console.log(sum);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 22:32









          eag845eag845

          878611




          878611

























              0














              You could calculate and sum in a single loop by taking a destructuring assignment for the needed properties and then add to sum the price.



              $scope.multiplyResult = $scope.result
              .reduce((sum, { Cost, Quantity, Discount }) => sum + Cost * Quantity + Discount, 0);





              share|improve this answer


























                0














                You could calculate and sum in a single loop by taking a destructuring assignment for the needed properties and then add to sum the price.



                $scope.multiplyResult = $scope.result
                .reduce((sum, { Cost, Quantity, Discount }) => sum + Cost * Quantity + Discount, 0);





                share|improve this answer
























                  0












                  0








                  0






                  You could calculate and sum in a single loop by taking a destructuring assignment for the needed properties and then add to sum the price.



                  $scope.multiplyResult = $scope.result
                  .reduce((sum, { Cost, Quantity, Discount }) => sum + Cost * Quantity + Discount, 0);





                  share|improve this answer












                  You could calculate and sum in a single loop by taking a destructuring assignment for the needed properties and then add to sum the price.



                  $scope.multiplyResult = $scope.result
                  .reduce((sum, { Cost, Quantity, Discount }) => sum + Cost * Quantity + Discount, 0);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 '18 at 22:30









                  Nina ScholzNina Scholz

                  178k1491156




                  178k1491156






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270965%2fsubstract-an-element-of-an-array-after-multiply-two-elements-on-the-same-array%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Lugert, Oklahoma