简体   繁体   中英

Sum of array values at dictionary positions only - Javascript

I have x amounts of data sets to post into a line graph, and I want an additional line produced that is the sum of all dictionary entries at specific positions (in this instance, it's a date of entry). I only have 1 dictionary since the data is pulled prior from a database - dictionary called `all_users_experiences".

I'm trying to add the values together at each dictionary position and push into another array so i can then divide each position by the number of users to get the sum of each position, but for the life of me I can only find examples of regular arrays having positions appended, not dictionary values. New to JavaScript so any help would be wonderful!

arrays: (2) [Array(30), Array(30)]

array contents (each number having a key is causing me the issue!):

(30) [1, 2, 5, 8, 9, 9, 4, 3, 3, 5, 6, 2, 2, 1, 5, 6, 7, 7, 7, 7, 6, 6, 4, 3, 4, 3, 2, 5, 7, 7]

(30) [5, 5, 6, 6, 7, 5, 5, 4, 3, 2, 1, 1, 6, 7, 8, 7, 7, 7, 6, 7, 8, 9, 7, 5, 6, 6, 7, 8, 7, 6]

I've successfully made this work with regular lists of number, but not dictionary values!

Thank you so much in advance.

EDIT: Orignal unaltered Data was parsed into JS via python JSON can only show the pulled data from this file for sensitivity reasons. numbers in original question content is what is being collated with below.

HTML 控制台快照:

Prior code tried (1 attempt of many...):

  console.log(all_users_experiences) //Producing 2 arrays
  console.log(all_users_experiences[0])
  console.log(all_users_experiences[1])

  var total_users = Object.keys(linegraph_data).length;

  user_exp_aggregate = []

  for (var i = 0; i < total_users.length; i++){
    user_exp_aggregate.push(all_users_experiences[i][0] + all_users_experiences[i][1]);
    console.log(user_exp_aggregate)
  }
  console.log("Total list: " + user_exp_aggregate)

EDIT2

Amended as per suggestion by Bergi but no data is being added to new array still :

  user_exp_aggregate = []
  console.log("Initial list: "+ user_exp_aggregate)
  for (var i = 0; i < total_users.length; i++){
    user_exp_aggregate.push(all_users_experiences[0][i] + all_users_experiences[1][i]);
    console.log("updated list: " + user_exp_aggregate)
  }
  console.log("Total list: " + user_exp_aggregate)

Amended snapshot of result

example of how current data from arrays is being used in graph (trying to create a 3rd line graph data set showing the average between all users!):

enter image description here

This is probably what you are looking for:

 all_users_experiences = [ [1, 2, 5, 8, 9, 9, 4, 3, 3, 5, 6, 2, 2, 1, 5, 6, 7, 7, 7, 7, 6, 6, 4, 3, 4, 3, 2, 5, 7, 7], [5, 5, 6, 6, 7, 5, 5, 4, 3, 2, 1, 1, 6, 7, 8, 7, 7, 7, 6, 7, 8, 9, 7, 5, 6, 6, 7, 8, 7, 6] ] user_exp_aggregate = [] //Loop to the full length for each for (var i = 0; i < all_users_experiences[0].length; i++) { user_exp_aggregate.push(all_users_experiences[0][i] + all_users_experiences[1][i]); } console.log("Total list: " + user_exp_aggregate) //If you want to divide each value by total user count user_exp_average = [] //var total_users = Object.keys(linegraph_data).length; var total_users = 2 //Assuming total users are 2 for (var i = 0; i < user_exp_aggregate.length; i++) { user_exp_average.push(user_exp_aggregate[i] / total_users); } console.log("Average: " + user_exp_average)

The issue happening in edit 2 part of code it that, the loop is only running till total user length. But as per your specification, you need to loop through all experiences to get the aggregate array and then divide each value by total number of users to get the average.

Hope it helps. Revert for any doubts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM