2015-03-19 169 views
6

嗨,我試圖從JSON數組中構建2個數組。如何從jSon對象構建數組

{ 
    "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" 
    }, 


} 

這是JSON數組,

所以我想建立2個陣列。

1陣列,其中status=='booked' nOR status=='unavailable'和建造起來的jQuery陣列這樣

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']; 

另一個那些元件的保持只鍵(在這種情況下的時間)正在建立與那些天的日期另一個陣列,其中status=='available' AND price > '100$'

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

我怎樣才能實現這與jQuery?

+0

可能重複的[轉換數組到JSON](http://stackoverflow.com/questions/2295496/convert-array-to-json ) – arkoak 2015-03-19 12:24:11

+0

發生了什麼事的答案????? – Johnny 2015-03-19 12:51:58

+0

@watcher是我還是在這之前答案? – Johnny 2015-03-19 12:56:18

回答

4

如果j是您的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); 

產量:

["2015-03-24", "2015-03-25", "2015-03-26"] 
["2015-04-10", "2015-04-11", "2015-05-01", "2015-05-02", "2015-05-03"] 
+0

thanx無關,這是我的答案看着.. – Johnny 2015-03-19 14:17:21

0

試試這個,

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); 
    } 
}) 
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); 
}); 
1

你可以有一個更通用的方法,這可能是有助於適應其他你的場景,沒有jQuery的依賴。數據過濾的一個小功能:

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; 
} 

給予,你現在就可以編寫代碼,如:

// 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) 
       (); 

哪裏obj是所有數據的對象。

該命令將返回兩個數組,一個用於定義每個謂詞,包含所有對象 - 因爲如果要訪問某些屬性,可能會很有用,再次過濾。如果你只想要的鍵,那麼你可以簡單地做:

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