1

I have the following variable:

response = "['2013-04-20', 10568]", "['2013-04-21', 4566]", "['2013-04-22', 14228]", "['2013-04-23', 5056]", "['2013-04-24', 25837]", "['2013-04-25', 39723]", "['2013-04-26', 37297]", "['2013-04-27', 23482]", "['2013-04-28', 31371]", "['2013-04-29', 25088]", "['2013-04-30', 27726]", "['2013-05-01', 26287]", "['2013-05-02', 15988]", "['2013-05-03', 34628]", "['2013-05-04', 13415]", "['2013-05-05', 24566]", "['2013-05-06', 33443]", "['2013-05-07', 15676]", "['2013-05-08', 18143]", "['2013-05-09', 28611]", "['2013-05-10', 46631]", "['2013-05-11', 24315]", "['2013-05-12', 15183]", "['2013-05-13', 13154]", "['2013-05-14', 9185]"

send to me by a php script. I do the following operations on it:

var organicJson = response.replace(/"/g,'');
organicJson = "[" + organicJson + "]";
organicJson = organicJson.replace(/'/g,'"');
var myOrganicData = JSON.parse(organicJson);

to obtain the desired form (for further scopes) which is a multidimensional-array. How it is possible in JavaScript to iterate through it and get the biggest value (in the above example I think is 46631) and also the corresponding date (ex. 2013-05-10)? Here is an example on jsfiddle: http://jsfiddle.net/DanielaVaduva/PYEhK/

4
  • you can write a normal for loop. Commented May 20, 2013 at 11:34
  • What's in the variable response? Is it actually an array? If so, is it an array of strings, or an array of arrays? Commented May 20, 2013 at 11:34
  • @Guffa Response it's just a string that I send back to my JavaScript with a PHP echo function and then I make the eventual operations with it... Commented May 20, 2013 at 11:36
  • @DanielacostinaVaduva: What is the string, then? The code that you show uses the comma operator between a lot of strings, so you would end up with just response = "['2013-05-14', 9185]";. Commented May 20, 2013 at 11:38

3 Answers 3

2

I would recommend to use Underscore library, then it is easy to find a max :

var m = _.max(myOrganicData, function(item){
   return item[1];
})

alert(m); 
alert(m[1]); 

Here is the code..

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

3 Comments

Is there an alternative not to use Underscore 1.4.3 ?
well, then probably just write your own loop.
I will use Underscore library for now, thank you very much for pointing me at this, I really appreciate. :)
1

If response is indeed a multidimensional array then this would work, i'm sure there is a neater or more efficient way of achieving this but this should be a push in the right direction

var response = [['2013-04-20', 10568], ['2013-04-21', 4566], ['2013-04-22', 14228], ['2013-04-23', 5056], ['2013-04-24', 25837]];

function returnLargest(multiArr){
  var lastNum, output = [];

  for(var i=0; i<multiArr.length; i++){ 
    if(!lastNum){ 
       lastNum = multiArr[i][1];
       output[0] = multiArr[i][0]; 
    } else if(lastNum < multiArr[i][1]){
      lastNum = multiArr[i][1];
      output[0] = multiArr[i][0];
    }
  }
  output.push(lastNum);
  return output;  //In this case ['2013-04-24', 25837] 
}

Comments

1

I've got this function:

function maxBy(array, fn) {
    return array.map(function(x) {
        return [x, fn(x)]
    }).reduce(function(max, x) {
        return x[1] > max[1] ? x : max
    })[0]
}

Usage:

 maxItem = maxBy(array, function-to-return-a-property)

In your example:

 maxItem = maxBy(myOrganicData, function(x) { return x[1] })

http://jsfiddle.net/PYEhK/2/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.