我有这个数组obejcts
var customers =
[{"id":"1", "name":"John","position":"CEO","office":"NY", "active": "1"},
{"id":"2", "name":"Michael","position":"CEO","office":"NY", "active": "1"},
{"id":"3", "name":"Wanda","position":"CEO","office":"NY", "active": "0"},
{"id":"3", "name":"Novak","position":"CEO","office":"NY", "active": "0"}];
我需要的是获得该阵列中的客户数量,
var indexID = 4;
还有一个看起来像这样的新数组
var newArray =
[{"id", "name","position","office"}];
这个新数组只收集一次标题而没有活动
答案 0 :(得分:2)
首先,json无效。它应该是:
var customers =
[{"id":"1", "name":"John","position":"CEO","office":"NY", "active": "1"},
{"id":"2", "name":"Michael","position":"CEO","office":"NY", "active": "1"},
{"id":"3", "name":"Wanda","position":"CEO","office":"NY", "active": "0"},
{"id":"3", "name":"Novak","position":"CEO","office":"NY", "active": "0"}];
您可以使用length
这样计算:
var count = customers.length;
并且您只能使用map()
创建具有所需属性的新数组:
var newArray = customers.map(function(item,index){
return { "id": item.id, "name": item.name,"position": item.position }
});
答案 1 :(得分:1)
我很确定。长度就是你要找的......
var indexID = customers.length;
var newArray = Object.keys(customers[0]);
感谢此链接提供参考资料: How do I enumerate the properties of a JavaScript object?