如何从 json 对象构建数组
Posted
技术标签:
【中文标题】如何从 json 对象构建数组【英文标题】:How to build an array from a jSon object 【发布时间】:2015-05-22 13:28:17 【问题描述】:我正在尝试从 JSON 数组构建 2 个数组。
"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 实现这一点?
【问题讨论】:
Convert array to JSON 的可能重复项 答案怎么了???? @watcher 是我还是以前的答案??? 它被所有者删除了,我想是因为它是用php而不是javascript编写的。 是的,我删除了,因为我是用PHP做的,太可惜了,如果还有时间我必须尝试用jQuery做...... 【参考方案1】:如果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"]
【讨论】:
【参考方案2】:你可以有一个更通用的方法,它可以用于适应你的其他场景,而不依赖于 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
是包含所有数据的对象。
该命令将返回两个数组,一个用于定义的每个谓词,以及所有对象——因为如果你想访问某些属性,它可能很有用,o 再次过滤。如果你只想要钥匙,那么你可以简单地这样做:
var notAvailableDates = Object.keys(results[0]);
【讨论】:
【参考方案3】:试试这个,
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);
)
【讨论】:
【参考方案4】: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);
);
【讨论】:
以上是关于如何从 json 对象构建数组的主要内容,如果未能解决你的问题,请参考以下文章
如何将嵌套的 JSON 对象转换为数组 Spring Boot?