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.

4
  • 1
    there is no products[i].NewColumn, it should be column 1,2,3.... like products[i].NewColumn_1 Commented Sep 16, 2014 at 12:29
  • yes instead that i need max one. means fieldValue Commented Sep 16, 2014 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)? Commented Sep 16, 2014 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 Commented Sep 16, 2014 at 12:56

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

i am getting max value. just i need to give like products[i].fieldValue =jsonData[i].Title; i am unable to do that.
products[i]['NewColumn_' + maximum] = data
@Ram, please mark the answer as correct and close the post, thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.