1

I have, for example, the following array of objects:

data = [{id:name1, num1:12, num2:8},
        {id:name2, num1:5, num2:13},
        {id:name3, num1:21, num2:4}];

I am wondering, how can I refer only to property "num1" of all objects or "num2", considering, that I don't know the exact name of the property. So if I want to find the Sum of all num2 I cannot do it simply writing the following piece of code:

data.forEach(function(obj) {
Sum += obj.num2;
};

Cause "num2" may not be called "num2" but any other name, depending on the generated report (data is taken from the Microstrategy reporting system). And it is inconvenient to change the code every time just to meet the property name.

Any help is highly appreciated! :)

Slava32
  • 161
  • 10
  • 1
    So how would identify the "column" to sum? By position? – Dan Mar 24 '16 at 16:04
  • Why don't you put all the numbers in an array inside the object instead of the current structure? Example: `data = [{id: 'x', num: [12, 8]}];` – Derek Pollard Mar 24 '16 at 16:04
  • Will the custom name for the "num2" key always be placed as the 3rd key in every object? – Jonathan Nielsen Mar 24 '16 at 16:06
  • Use `object[variable_with_prop_name]`. BTW `object.property` is just a shortcut to `object["property"]`. – Oleg V. Volkov Mar 24 '16 at 16:09
  • Can you clarify which column you want to sum up – user7 Mar 24 '16 at 16:18
  • @OliverQueen, because I get the data from the report in csv format first, and in that case it will have every value in the row in separate column, with own property name :/ – Slava32 Mar 24 '16 at 16:23
  • @JonathanNielsen, yes, it will be always placed as the 3rd key in every object. – Slava32 Mar 24 '16 at 16:25
  • @user7, I want to be able to sum up the 2nd and 3rd columns with numeric values. To get Sum_num1 and Sum_num2, for example. – Slava32 Mar 24 '16 at 16:28
  • I guess unless you have the values in an array, you cannot do it as in javascript, properties order in an object cannot be guaranteed. – user7 Mar 24 '16 at 16:33
  • See: [Elements order in a “for (… in …)” loop](http://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop) "Currently all major browsers loop over the properties of an object in the order in which they were defined." – Roberto Mar 24 '16 at 16:34
  • @user7, well, I did it, so it's possible :) Everything works now. – Slava32 Mar 29 '16 at 11:52

2 Answers2

0

This may be overkill, but this is what I would do to create a Sum object from the original

data = [{
  id: 'name1',
  num1: 12,
  num2: 8
}, {
  id: 'name2',
  num1: 5,
  num2: 13
}, {
  id: 'name3',
  num1: 21,
  num2: 4
}];

var Sum = {};
data.forEach(function(obj) {

  var keys = Object.keys(obj);
  keys.forEach(function(k) {
  if( i )
    if (Sum.hasOwnProperty(k)) {
      Sum[k] += obj[k];
    } else {
      Sum[k] = obj[k];
    }
  })
})

console.log(Sum);

https://jsfiddle.net/d6vttp9v/

Note: this also sums the string field, so its kind of nonsense, but you could easily specify an array of column to exclude (exercise for the reader).

So this answer doesn't get you exactly what you want (I'm not sure exactly what you want), but hopefully its a good step in right direction.

Dan
  • 8,963
  • 4
  • 18
  • 34
-2

You can access an objects properties like so:

var x = { someProp1: 'x', someProp2: 'y' };
for(prop in x) {
  console.log(prop);
}

Heres a handy reference

If this question is more generally about schema changes in JSON objects and whats the best approach to that, heres an angle I use.

for example the ajax call getPeople

myApp.ajaxSchemas.getPeople = {
  NAME: 'Name',
  AGE: 'Age'
};

I would then refer to the schema in my code:

someObject[myApp.ajaxSchemas.getPeople.NAME];

This means that when theres a schema change it is managed in one place, the myApp.ajaxSchemas.getPeople definition.

I hope this helps.

Rhys

Community
  • 1
  • 1
Rhys Bradbury
  • 1,566
  • 11
  • 23