-1

I am trying to get the value of "current" from this JSON data. I need to be in one line without any loop. Any suggestions?

     var data=[

     {"id":728,"acc":50,"date":"03-10-2017 05:45"},
     {"id":727,"acc":30,"date":"03-09-2017 21:00"},
     {"id":726,"acc":50,"date":"03-09-2017 05:45"},
     {"id":725,"acc":30,"date":"03-08-2017 21:00"},
     {"id":724,"acc":50,"date":"03-08-2017 05:45"},
     {"id":723,"acc":30,"date":"03-07-2017 21:15"},
     {"id":722,"acc":50,"date":"03-07-2017 05:45"},
     {"id":721,"acc":30,"date":"03-06-2017 21:00"},
     {"id":720,"acc":50,"date":"03-06-2017 05:45"},
     {"id":719,"acc":30,"date":"03-03-2017 21:00"},
     {"id":718,"acc":50,"date":"03-03-2017 05:45"},
     {"id":717,"acc":30,"date":"03-02-2017 21:00"},
     {"id":716,"acc":50,"date":"03-02-2017 05:45"},
     {"id":715,"acc":30,"date":"03-01-2017 21:00"},
     {"id":714,"acc":50,"date":"03-01-2017 05:45"},
     {"id":713,"acc":30,"date":"02-28-2017 21:00"},

     [
      {"current":"03-10-2017 05:45"}
     ]

     ];

     var mval = data[0].current;
     alert(mval);

     //alert(data[1].current);

Thank you!

Andre
  • 819
  • 3
  • 15
  • 30

5 Answers5

0

A safe solution, that would find only the array with a property current would be

data.find(v => v.length && v[0].current)[0].current;
baao
  • 71,625
  • 17
  • 143
  • 203
  • 1
    pop modifies data, probably this is something OP would not want. – burkay Mar 10 '17 at 20:28
  • Assuming ordering of the array is always consistent – Rikin Mar 10 '17 at 20:30
  • @burkay true. Edited the answer – baao Mar 10 '17 at 20:32
  • This will throw an error if none of the array's elements is an array. But semantically, `find` probably *does* make more sense than `some`. – Rick Hitchcock Mar 10 '17 at 21:33
  • That's true @RickHitchcock. OP asked for a single line solution and showed an array that won't throw for above code. However, you are completely right, it is not a failure proof solution. – baao Mar 10 '17 at 21:38
  • BTW. @RickHitchcock please explain me how you could find a solution for this question: http://stackoverflow.com/questions/42726436/resorting-and-removing-number-gaps-using-javascript-array#comment72572589_42726436 – baao Mar 10 '17 at 21:39
  • Not sure I understand. Are you asking how I came up with my solution or how I was able to understand the question? – Rick Hitchcock Mar 10 '17 at 21:42
  • the latter @RickHitchcock – baao Mar 10 '17 at 21:42
  • Oh, haha, it *was* confusing based on the description, so I just compared the expected output to the input and it made sense. – Rick Hitchcock Mar 10 '17 at 21:46
  • Even though I understand the code in your answer, I don't understand the question... :D @RickHitchcock I'll have another look on it tomorrow when I didn't have worked 12 hours before reading it :-) – baao Mar 10 '17 at 21:51
0
data[data.length - 1][0].current
TwilightTitus
  • 190
  • 1
  • 9
0

if the data is always the same this is your one liner

var mval = data[data.length - 1][0].current;

if the data might change you can loop trough the array and find the inner array like this

var myVal;
    for(obj in data) {
        if(data[obj].constructor === Array) {
            myVal = data[obj];
        }
    }

    alert(myVal[0].current);

window.onload = function () {
    var data = [

        { "id": 728, "acc": 50, "date": "03-10-2017 05:45" },
        { "id": 727, "acc": 30, "date": "03-09-2017 21:00" },
        { "id": 726, "acc": 50, "date": "03-09-2017 05:45" },
        { "id": 725, "acc": 30, "date": "03-08-2017 21:00" },
        { "id": 724, "acc": 50, "date": "03-08-2017 05:45" },
        { "id": 723, "acc": 30, "date": "03-07-2017 21:15" },
        { "id": 722, "acc": 50, "date": "03-07-2017 05:45" },
        { "id": 721, "acc": 30, "date": "03-06-2017 21:00" },
        { "id": 720, "acc": 50, "date": "03-06-2017 05:45" },
        { "id": 719, "acc": 30, "date": "03-03-2017 21:00" },
        { "id": 718, "acc": 50, "date": "03-03-2017 05:45" },
        { "id": 717, "acc": 30, "date": "03-02-2017 21:00" },
        { "id": 716, "acc": 50, "date": "03-02-2017 05:45" },
        { "id": 715, "acc": 30, "date": "03-01-2017 21:00" },
        { "id": 714, "acc": 50, "date": "03-01-2017 05:45" },
        { "id": 713, "acc": 30, "date": "02-28-2017 21:00" },

        [
            { "current": "03-10-2017 05:45" }
        ]

    ];

    var myVal;
    for(obj in data) {
        if(data[obj].constructor === Array) {
            myVal = data[obj];
        }
    }

    alert(myVal[0].current);

}
Pato Salazar
  • 1,447
  • 12
  • 21
0

Assuming the only "Array" type object in data is the one containing the object with the key "current", you can use this:

var current = data.filter(function(x){ return  Object.prototype.toString.call( x ) === '[object Array]';} )[0][0].current;

Take a look at Check if object is array?

Community
  • 1
  • 1
burkay
  • 1,075
  • 1
  • 10
  • 20
0

Assuming current can be anywhere in the array, you'll need to iterate to find it:

var current;
data.some(function(e) {
  if(e[0] && e[0].current) {  //make sure it's an array with an object key "current"
    current = e[0].current;
  }
});

var data=[

     {"id":728,"acc":50,"date":"03-10-2017 05:45"},
     {"id":727,"acc":30,"date":"03-09-2017 21:00"},
     {"id":726,"acc":50,"date":"03-09-2017 05:45"},
     {"id":725,"acc":30,"date":"03-08-2017 21:00"},
     {"id":724,"acc":50,"date":"03-08-2017 05:45"},
     {"id":723,"acc":30,"date":"03-07-2017 21:15"},
     {"id":722,"acc":50,"date":"03-07-2017 05:45"},
     {"id":721,"acc":30,"date":"03-06-2017 21:00"},
     {"id":720,"acc":50,"date":"03-06-2017 05:45"},
     {"id":719,"acc":30,"date":"03-03-2017 21:00"},
     {"id":718,"acc":50,"date":"03-03-2017 05:45"},
     {"id":717,"acc":30,"date":"03-02-2017 21:00"},
     {"id":716,"acc":50,"date":"03-02-2017 05:45"},
     {"id":715,"acc":30,"date":"03-01-2017 21:00"},
     {"id":714,"acc":50,"date":"03-01-2017 05:45"},
     {"id":713,"acc":30,"date":"02-28-2017 21:00"},

     [
      {"current":"03-10-2017 05:45"}
     ]

     ];

var current;
data.some(function(e) {
  if(e[0] && e[0].current) {
    current = e[0].current;
  }
});

console.log(current);
Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79