How to get the sum of number from an array of objects, using pure JS?

Egzon Berisha

I want to show the total number of Likes using only pure JS. I grabbed the numbers from the JSON file but I don't know how to get the Total number of likes, I tried to use reduce method it didn't work maybe i am doing in the wron way

This is what I want to do click here for the photo This is my code

function photographerWork(JsonData, homeElement){
   const homeElt = homeElement.id;
   JsonData.media.forEach(element => {   
   if(homeElt == element.photographerId){
       const domDiv = document.getElementById('photographer-work');
       const newDiv = document.createElement("div");
       /////  the code i'am trying  ////
       const allTheLiks = element.likes
       console.log(allTheLiks)
       //////////////////////
       const workTemplate = `         
           <div class="photo-box"> 
               <div class="photo">
                   ${videoOrImage(element.image, element.video, element)}
               </div>   
               <div class="text">
                   <p> ${element.tags}<b>${element.price} €  &nbsp ${element.likes} <i class="fas fa-heart"></i></b></p>
               </div>
           </div>
           `
       newDiv.innerHTML = workTemplate;
       domDiv.appendChild(newDiv);
       likesAndPrice(element, allTheLiks); 
     }
 })
} 

This is my Result click hereenter code here

This is my JSON file example

 "media": [
      {
        "id": 342550,
        "photographerId": 82,
        "image": "../Photos/Tracy/Fashion_Yellow_Beach.jpg",
        "tags": ["fashion"],
        "likes": 62,
        "date": "2011-12-08",
        "price": 55
      },
      {
        "id": 8520927,
        "photographerId": 82,
        "image": "../Photos/Tracy/Fashion_Urban_Jungle.jpg",
        "tags": ["fashion"],
        "likes": 11,
        "date": "2011-11-06",
        "price": 55
      },
      {
        "id": 9025895,
        "photographerId": 82,
        "image": "../Photos/Tracy/Fashion_Pattern_on_Pattern.jpg",
        "tags": ["fashion"],
        "likes": 72,
        "date": "2013-08-12",
        "price": 55
      },
      {
        "id": 9275938,
        "photographerId": 82,
        "image": "../Photos/Tracy/Event_WeddingGazebo.jpg",
        "tags": ["events"],
        "likes": 69,
        "date": "2018-02-22",
        "price": 55
      },
      {
        "id": 2053494,
        "photographerId": 82,
        "image": "../Photos/Tracy/Event_Sparklers.jpg",
        "tags": ["events"],
        "likes": 2,
        "date": "2020-05-25",
        "price": 55
      },
Alex

Before you enter the forEach() create a local variable sum

let sum = 0;

Then inside your forEach() do the following

sum += element.likes;

Then when the loop has finished in sum will be the total amount of likes.

Updated code.

function photographerWork(JsonData, homeElement){
   let sum = 0;
   const homeElt = homeElement.id;
   JsonData.media.forEach(element => {   
   if(homeElt == element.photographerId){
       const domDiv = document.getElementById('photographer-work');
       const newDiv = document.createElement("div");
       /////  the code i'am trying  ////
       const allTheLiks = element.likes
       console.log(allTheLiks)
       //////////////////////

       sum += element.price;

       const workTemplate = `         
           <div class="photo-box"> 
               <div class="photo">
                   ${videoOrImage(element.image, element.video, element)}
               </div>   
               <div class="text">
                   <p> ${element.tags}<b>${element.price} €  &nbsp ${element.likes} <i class="fas fa-heart"></i></b></p>
               </div>
           </div>
           `
       newDiv.innerHTML = workTemplate;
       domDiv.appendChild(newDiv);
       likesAndPrice(element, allTheLiks); 
     }

})
 // here you have the sum now 
    console.log(sum);
} 

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to get a sum of an array in Nodejs using foreach?

分類Dev

How to get pure text from python email using imaplib

分類Dev

How to get a the sum of multiple arrays within an array of objects?

分類Dev

How to get unique an array of objects back from a complex an array of objects?

分類Dev

How to find sum objects inside an array using mongoose ,node ,

分類Dev

How to find sum objects inside an array using mongoose ,node ,

分類Dev

How to get an Array of Objects from Firestore in Swift?

分類Dev

Get union of keys of all objects in js array using reduce

分類Dev

How to get a particular attribute from an array of array objects?

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

How to get `Month Name` from `Month number` using `moment.js`

分類Dev

How to get only one value in Javascript array of objects using for of and for in statements?

分類Dev

Sum values of objects which are inside an array with underscore.js and reduce

分類Dev

how would you make it so that it grabs a random number and uses it to grab objects from an array

分類Dev

How to store the values of a JSON response to an API Call in as many variables as the (unknown) number of objects using node.js

分類Dev

Adding JS objects to an array using a cursor loop

分類Dev

Get object from array with NSDictionary objects

分類Dev

PHP - How to get the sum of a multidimensional array?

分類Dev

How to get sum of array in foreach in laravel 6

分類Dev

how to get the array output from a js function in java 8?

分類Dev

How do I sum up all remaing amount after get max value from array?

分類Dev

Get average value from array consisting of objects based on objects fields

分類Dev

Get number of occurences from an array in Laravel

分類Dev

How to get json value from json array using javascript

分類Dev

How get data from array in json item using angularjs

分類Dev

Get n number of points from angle 0 to 2π using an array operator like map or reduce?

分類Dev

How to get all values of objects inside array

分類Dev

How to search for a value in array of objects and get it in Laravel?

分類Dev

How do I deploy REST API using an imported array of JS objects?

Related 関連記事

  1. 1

    How to get a sum of an array in Nodejs using foreach?

  2. 2

    How to get pure text from python email using imaplib

  3. 3

    How to get a the sum of multiple arrays within an array of objects?

  4. 4

    How to get unique an array of objects back from a complex an array of objects?

  5. 5

    How to find sum objects inside an array using mongoose ,node ,

  6. 6

    How to find sum objects inside an array using mongoose ,node ,

  7. 7

    How to get an Array of Objects from Firestore in Swift?

  8. 8

    Get union of keys of all objects in js array using reduce

  9. 9

    How to get a particular attribute from an array of array objects?

  10. 10

    How to get the combination of array values from nested arrays in an array of objects

  11. 11

    How to get `Month Name` from `Month number` using `moment.js`

  12. 12

    How to get only one value in Javascript array of objects using for of and for in statements?

  13. 13

    Sum values of objects which are inside an array with underscore.js and reduce

  14. 14

    how would you make it so that it grabs a random number and uses it to grab objects from an array

  15. 15

    How to store the values of a JSON response to an API Call in as many variables as the (unknown) number of objects using node.js

  16. 16

    Adding JS objects to an array using a cursor loop

  17. 17

    Get object from array with NSDictionary objects

  18. 18

    PHP - How to get the sum of a multidimensional array?

  19. 19

    How to get sum of array in foreach in laravel 6

  20. 20

    how to get the array output from a js function in java 8?

  21. 21

    How do I sum up all remaing amount after get max value from array?

  22. 22

    Get average value from array consisting of objects based on objects fields

  23. 23

    Get number of occurences from an array in Laravel

  24. 24

    How to get json value from json array using javascript

  25. 25

    How get data from array in json item using angularjs

  26. 26

    Get n number of points from angle 0 to 2π using an array operator like map or reduce?

  27. 27

    How to get all values of objects inside array

  28. 28

    How to search for a value in array of objects and get it in Laravel?

  29. 29

    How do I deploy REST API using an imported array of JS objects?

ホットタグ

アーカイブ