calculating array of array values in index manner

arrangement and rearrangement of elements of array in java
reorder an array according to given indexes
how to sort an array and keep track of the index in java
reorder an array according to given indexes in c++
reorder an array according to given indexes python
reorder an array according to given indexes javascript
reorder an array according to given indexes leetcode
how to sort an array and keep track of the index in c

I am receiving an object from server that seems something like

    var response = [
         {
          "id":"1",
          "series":{
                    "eoy":[10,20,30,40,50,60],
                    "eom":[20,30,40,50,60],
                    "now":[30,40,50,60,70,80]
                   }
         }
         {
          "id":"2",
          "series":{
                   "eoy":[30,40,20,50,10],
                   "eom":[20,10,50,70,20,50],
                   "now":[30,10,70,20]
                   }
         }
    ]

inside series, there are many keys and values. just for problem purpose I wrote 3 pairs. Now I want to add those arrays in such a manner that I will get the result as a object which should be like

var result = {
     "eoy":[40,60,50,90,60,60],
     "eom":[40,40,90,120,80,50],
     "now":[60,50,120,80,70,80]
}

In this, I'm adding adding all the eoys and making it as one array and doing with every series member. Is there any way to make a single object of all calculated values.

You can use reduce to group the array and objects. Use Object.entries to convert the object into array. And use forEach to loop thru your arrays.

let arr = [{"id": "1","series": {"eoy": [10, 20, 30, 40, 50, 60],"eom": [20, 30, 40, 50, 60],"now": [30, 40, 50, 60, 70, 80]}},{"id": "2","series": {"eoy": [30, 40, 20, 50, 10],"eom": [20, 10, 50, 70, 20, 50],"now": [30, 10, 70, 20]}}];

let result = arr.reduce((c, {series}) => {
  Object.entries(series).forEach(([k, v]) => {
    c[k] = c[k] || [];
    v.forEach((o, i) => {
      c[k][i] = c[k][i] || 0;
      c[k][i] += o;
    });
  })
  return c;
}, {});

console.log(result);

Reorder an array according to given indexes, Traverse the given array and put all elements at their correct place in temp[] using index[]. Finally copy temp[] to arr[] and set all values of index[i] as i. C++. The use of index arrays ranges from simple, straightforward cases to complex, hard-to-understand cases. For all cases of index arrays, what is returned is a copy of the original data, not a view as one gets for slices. Index arrays must be of integer type. Each value in the array indicates which value in the array to use in place of the index.

You can use reduce, like this:

const input=[{"id":"1","series":{"eoy":[10,20,30,40,50,60],"eom":[20,30,40,50,60],"now":[30,40,50,60,70,80]}},{"id":"2","series":{"eoy":[30,40,20,50,10],"eom":[20,10,50,70,20,50],"now":[30,10,70,20]}}];
const output = input.reduce((a, { series }) => {
  Object.entries(series).forEach(([key, arr]) => {
    if (!a[key]) a[key] = [];
    const sumArr = a[key];
    arr.forEach((num, i) => sumArr[i] = (sumArr[i] || 0) + num);
  });
  return a;
}, {});
console.log(output);
{
 "eoy":[40,60,50,90,60,60],
 "eom":[40,40,90,120,80,50],
 "now":[60,50,120,80,70,80]
}

Program for average of an array (Iterative and Recursive , C++ program to calculate average of array elements. #include The idea is to pass index of element as an additional parameter and recursively compute sum. * Average value of array elements would be. * sum of all elements/total number of elements. */. //calculate sum of all array elements. int sum = 0; for(int i=0; i < numbers.length ; i++) sum = sum + numbers[i];

Use Array.reduce, Array.forEach and Object.entries

var arr = [{"id":"1","series":{"eoy":[10,20,30,40,50,60],"eom":[20,30,40,50,60],"now":[30,40,50,60,70,80]}},{"id":"2","series":{"eoy":[30,40,20,50,10],"eom":[20,10,50,70,20,50],"now":[30,10,70,20]}}];

let result = arr.reduce((a, c) => {
  /* For each item in the array, iterate over each entry
   ** in series and sum it in the accumulator object. */
  Object.entries(c.series).forEach(([k, v]) => {
    a[k] = a[k] || []; 
    v.forEach((n, i) => a[k][i] = a[k][i] ? a[k][i] + n : n);
  });
  return a;
}, {});

console.log(result);

[PDF] An Introduction to Arrays and Array Processing, languages are that SAS array elements don't need to be contiguous, the same An alternative is to define arrays and use a loop to process the calculation for all This technique can simplify array usage by using natural values for the index. With a few exceptions, temporary data elements behave in a manner similar to. Circular array; Product of all Subarrays of an Array; Sliding Window Maximum : Set 2; Count of subsets with sum equal to X; Maximum number of unique values in the array after performing given operations; Calculate the Sum of GCD over all subarrays; Check if sum of Fibonacci elements in an Array is a Fibonacci number or not

You can do this with map:

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

var arr = [
 {
  "id":"1",
  "series": {
            "eoy":[10,20,30,40,50,60],
            "eom":[20,30,40,50,60],
            "now":[30,40,50,60,70,80]
           }
 },
 {
  "id":"2",
  "series":{
           "eoy":[30,40,20,50,10],
           "eom":[20,10,50,70,20,50],
           "now":[30,10,70,20]
           }
 }
];

console.log(arr.map(x=> x.series));

Arrays and Strings, Since, the indexing of an array starts from 0, if the 4th element needs to be accessed, on strings like copy, concatenation, comparison and finding the length. Calculating with arrays¶ Built-in python data types (lists, dictionaries, etc.) are fine for many applications. For mathematical operations, however, these types are not so flexible and fast. This is why the numpy module was created, which is now the base for most python scientific code.

Array methods, Here and in other array methods, negative indexes are allowed. They specify They are used to calculate a single value based on the array. Arrays . For general information on data types and arrays see the topic on Data Types. Arrays are used to store lists of related information. Your shopping list (Type = string); for the names of the students in a class (type = string) for the grades for the first exam (type = numbers).

Chapter 10: aCalcout - Array Calculation Output Record, The Array Calculation Output or "aCalcout" record is derived from the sCalcout (​string calcout) record DERIV, Derivative of array values, with respect to array index. The MLST and MLST fields are used in the same manner for the VAL field. The second programs takes the value of n (number of elements) and the numbers provided by user and finds the average of them using array. To understand these programs you should have the knowledge of following Java Programming concepts: 1) Java Arrays 2) For loop. Example 1: Program to find the average of numbers using array

array_search - Manual, array_search — Searches the array for a given value and returns the first If needle is a string, the comparison is done in a case-sensitive manner. if the given key or index exists in the array; in_array() - Checks if a value exists in an array. Array iteration methods are like a ‘gateway drug’. 1 They get many people hooked on functional programming. Because they’re just so darn useful. And most of these array methods are fairly simple to understand. Methods like .map() and .filter() take just one callback argument, and do fairly simple things.

Comments
  • Let me see your implementing code first @Yash Jain
  • Is there any way to do the same without using Object.entries()
  • Yes there is, is this fore node.js? @YashJain
  • I am making app in angular5 and when I tried Object.entries(). It says, "property 'entries()' doesn't exist on type 'ObjectConstructor' "
  • You can check to add stackoverflow.com/questions/42410029/…
  • I added another sample code block without Object.entries() please check
  • can we do the same without using Object.entries() coz when I tried to do the same in my angular5 app. It says " property 'entries()' doesn't exist on type 'ObjectConstructor' "
  • @YashJain - Updated.
  • Thanks Nikhil.... just want to know. Is there any performance difference between Object.entries() or what you used instead of Object.entries(). Asking this because I received answers using Object.entries()
  • @YashJain - I doubt there will be any performance issue. Object.entries is used because you can get both the key and value together otherwise with other methods like Object.keys, you will only get the keys and then to get the value you have to access the object again.
  • Thanks for info :)