0

My array contains NewColumn_1, NewColumn_2, NewColumn_3 etc... I am getting Max NewColumn_ Value in Array. Here NewColumn_SomeValue generating dynamically.

var products = [{
    "ProductID": 1,
    "ProductName": "Chai",
    NewColumn_1:'abc'
    NewColumn_2:'abc',
    NewColumn_3:'abc',
    NewColumn_4:'abc'
},{
    "ProductID": 2,
    "ProductName": "Chang",
    NewColumn_1:'def',
    NewColumn_2:'abc',
    NewColumn_3:'abc',
    NewColumn_4:'abc'
}]

var fieldValue = "NewColumn_" + (maxValueInArray);

products[i].NewColumn = jsonData[i].Title;

I need to products[i].fieldValue =jsonData[i].Title; i am unable to do. can any one help this.

Ram
  • 337
  • 5
  • 23
  • 1
    there is no `products[i].NewColumn`, it should be column 1,2,3.... like `products[i].NewColumn_1` – Bhushan Kawadkar Sep 16 '14 at 12:29
  • yes instead that i need max one. means fieldValue – Ram Sep 16 '14 at 12:30
  • What does mean `max` in this context? Max value from all object fields (it will be quite strange to compare strings)? Or max index of `NewColumn_X` (which is always `NewColumn_4`)? – Regent Sep 16 '14 at 12:37
  • No. Here i am generating Newcloums. we can say NewColumn_4 is max one. it can be NewColumn_5, NewColumn_6,NewColumn_7 etc – Ram Sep 16 '14 at 12:56

1 Answers1

0

Try this code below

var lengths = [], length;

for (var i = 0; i < products.length; i++) {
    length = 0;
    for (var property in products[i]) {
        if (property.indexOf("NewColumn_") == 0) {
            length++;
        }
    }
    lengths.push(length);
}

var maximum = Math.max.apply(null, lengths);

Reference: How to get an object's properties in JavaScript / jQuery? and JavaScript: min & max Array values?

Community
  • 1
  • 1
Mihir
  • 419
  • 3
  • 10