6

I am trying to build 2 arrays from JSON arrays.

{
    "2015-03-24": {
        "bind": 0,
        "info": "",
        "notes": "",
        "price": "150",
        "promo": "",
        "status": "available"
    },
    "2015-03-25": {
        "bind": 0,
        "info": "",
        "notes": "",
        "price": "150",
        "promo": "",
        "status": "available"
    },
    "2015-03-26": {
        "bind": 0,
        "info": "",
        "notes": "",
        "price": "150",
        "promo": "",
        "status": "available"
    },
    "2015-03-27": {
        "bind": 0,
        "info": "",
        "notes": "",
        "price": "100",
        "promo": "",
        "status": "available"
    },
    "2015-03-28": {
        "bind": 0,
        "info": "",
        "notes": "",
        "price": "100",
        "promo": "",
        "status": "available"
    },
    "2015-03-29": {
        "bind": 0,
        "info": "",
        "notes": "",
        "price": "100",
        "promo": "",
        "status": "available"
    },


    "2015-04-10": {
        "bind": 0,
        "info": "",
        "notes": "",
        "price": "",
        "promo": "",
        "status": "booked"
    },
    "2015-04-11": {
        "bind": 0,
        "info": "",
        "notes": "",
        "price": "",
        "promo": "",
        "status": "booked"
    },

    "2015-05-01": {
        "bind": 0,
        "info": "",
        "notes": "",
        "price": "",
        "promo": "",
        "status": "unavailable"
    },
    "2015-05-02": {
        "bind": 0,
        "info": "",
        "notes": "",
        "price": "",
        "promo": "",
        "status": "unavailable"
    },
    "2015-05-03": {
        "bind": 0,
        "info": "",
        "notes": "",
        "price": "",
        "promo": "",
        "status": "unavailable"
    },


}

This is the jSon array, so I want to build 2 arrays.

1 array holding only keys (in this case the date) of those element where status=='booked' nOR status=='unavailable' and build it in jQuery array like this

var array = ['2015-03-19', '2015-03-20', '2015-03-21', '2015-03-22', '2015-03-23', '2015-03-24', '2015-03-25', '2015-03-26', '2015-04-07', '2015-04-08', '2015-04-09', '2015-04-10'];

Another is building another array with the dates of those days where status=='available' AND price > '100$'

var array2 = [ '2015-03-25', '2015-03-26', '2015-04-07', '2015-04-08'];

How can I achieve this on with jQuery?

6
  • possible duplicate of Convert array to JSON Commented Mar 19, 2015 at 12:24
  • What happened to the answer ????? Commented Mar 19, 2015 at 12:51
  • @watcher was it me or it was an answer here before???? Commented Mar 19, 2015 at 12:56
  • it was deleted by the owner, I presume because it was written in PHP, and not javascript. Commented Mar 19, 2015 at 13:02
  • yes, I removed, because I did it in PHP, what a shame, I must try to do it in jQuery if there is still time... Commented Mar 19, 2015 at 13:07

4 Answers 4

5

If j is your json:

var a1 = [];
var a2 = [];
$.each( j, function( key, ob ) {
    if(ob.price > 100 && ob.status == 'available'){
        a1.push(key);
    }
    if(ob.status == 'booked' || ob.status == 'unavailable'){
        a2.push(key);
    }
});
console.log(a1);
console.log(a2);

Yields:

["2015-03-24", "2015-03-25", "2015-03-26"]
["2015-04-10", "2015-04-11", "2015-05-01", "2015-05-02", "2015-05-03"]
Sign up to request clarification or add additional context in comments.

Comments

1

You could have a more generic approach, that could be useful to adapt in other scenarios of yours, without dependencies from jQuery. A small function for data filtering:

function from(data) {
  var predicates = [];
  var results = [];

  function exec() {
    for (var k in data) {
      if (data.hasOwnProperty(k)) {
        for (var i = 0, l = predicates.length; i < l; i++) {
          if (predicates[i](data[k])) {
            results[i][k] = data[k]
          }
        }
      }
    }

    return results;
  }

  exec.get = function(predicate) {
    predicates.push(predicate);
    results.push({});
    return exec;
  }

  return exec;
}

Giving that, you can now write code like:

// predicates
function isNotAvailable(item) {
  return item.status === "unavailable" || item.status === "booked"
}

function isAvailableAndPriceGreater100(item) {
  return item.status === "available" && +item.price > 100
}

// results
var results = from(obj)
               .get(isNotAvailable)
               .get(isAvailableAndPriceGreater100)
               ();

Where obj is your object with all the data.

That command will returns two array, one for each predicate defined, with all the object – because it could be useful if you want to access to some properties, o filter again. If you want only the keys, at that point you can simply do:

var notAvailableDates = Object.keys(results[0]);

Comments

0

Try this,

var array1 = [];
var array2 = [];
$.each(data.items, function(key, val) {
   if((val.status == 'booked') || (val.status == 'unavailable')){
     array1.push(key);
   }
   if((val.status == 'available') && (val.price > 100)){
     array2.push(key);
   }
})

Comments

0
function obj_key_select(obj, func) {
    newArr = [];
    for(var index in obj) { 
        if (obj.hasOwnProperty(index)) {
            if(func(obj[index])) {
                newArr.push(index);
            }
        }
    }
    return newArr;
}

var dates = JSON.parse(jsonString);

var arrOne = obj_key_select(dates, function(element){
    return (element.status === 'booked' || element.status === 'unavailable'); 
});

var arrTwo = obj_key_select(dates, function(element){
    return (element.status === 'available' && parseInt(element.price) > 100); 
});

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.