Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2017
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reading values in JSON array

    Given the following array that is the result of a JSON.parse:

    Code:
    [object Object]
       [
          0: {
             [functions]: ,
             __proto__: { },
             ENS: 0,
             FRI: 7,
             MON: null,
             THU: null,
             TUE: null,
             WED: null
          },
          length: 1
       ]
    How do I check if any value is >0. I've been working on this for several hours.

  2. #2
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    2,916
    Thanks
    3
    Thanked 415 Times in 404 Posts
    Doesn't quite look like an array, but assuming it's a single entry array with an object in it... let's say that your parsed result is in a variable called "test"

    Code:
    var greaterThanZero = -1;
    for (var i in test[0]) {
      if (test[0][i] >0) {
        greaterThanZero = i;
        break;
      }
    }
    Will be negative 1 if none are greater than zero, returns the index of the first one found greater than zero and short-circuits out so we don't waste time iterating through all of them.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •