Javascript 根据对象的内容提取对象数组
在JavaScript中,我有一个JSON数组,如下所示:Javascript 根据对象的内容提取对象数组,javascript,json,extract,Javascript,Json,Extract,在JavaScript中,我有一个JSON数组,如下所示: var r = [{"name":"a","bool":false},{"name":"b","bool":false}, {"name":"c","bool":false},{"name":"a","bool":false}, {"name":"b","bool":false},{"name":"c","bool":true}, {"name":"a","bool":true}];
var r = [{"name":"a","bool":false},{"name":"b","bool":false},
{"name":"c","bool":false},{"name":"a","bool":false},
{"name":"b","bool":false},{"name":"c","bool":true},
{"name":"a","bool":true}];
我想构建另一个数组,其中包含name==“a”和bool==false的所有对象。我通过循环解决了这个问题,但我想知道是否还有其他方法
我该怎么办
[编辑]
我的循环是:
var rLen = r.length;
var newArray = [];
for(var i=0;i<rLen;i++) {
if(r[i].name=="a"&&r[i].bool==false) {
newArray.push(r[i]);
}
}
var-rLen=r.length;
var newArray=[];
对于(var i=0;i可能是
可能是
最终,它将始终通过内部循环来解决,例如
var r = [{"name":"a","bool":false},{"name":"b","bool":false},
{"name":"c","bool":false},{"name":"a","bool":false},
{"name":"b","bool":false},{"name":"c","bool":true},
{"name":"a","bool":true}];
var result = r.filter(function(item){
return item.name === 'a' && !item.bool;
});
如果查看polyfill,您会注意到循环:
if (!Array.prototype.filter) {
Array.prototype.filter = function(fun/*, thisArg*/) {
'use strict';
if (this === void 0 || this === null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function') {
throw new TypeError();
}
var res = [];
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) { // <--
if (i in t) {
var val = t[i];
// NOTE: Technically this should Object.defineProperty at
// the next index, as push can be affected by
// properties on Object.prototype and Array.prototype.
// But that method's new, and collisions should be
// rare, so use the more-compatible alternative.
if (fun.call(thisArg, val, i, t)) {
res.push(val);
}
}
}
return res;
};
}
if(!Array.prototype.filter){
Array.prototype.filter=函数(fun/*,thisArg*/){
"严格使用",;
if(this==void 0 | | this==null){
抛出新的TypeError();
}
var t=对象(本);
var len=t.length>>>0;
如果(乐趣的类型!=“功能”){
抛出新的TypeError();
}
var-res=[];
var thisArg=arguments.length>=2?参数[1]:无效0;
对于(var i=0;i
var r = [{"name":"a","bool":false},{"name":"b","bool":false},
{"name":"c","bool":false},{"name":"a","bool":false},
{"name":"b","bool":false},{"name":"c","bool":true},
{"name":"a","bool":true}];
var result = r.filter(function(item){
return item.name === 'a' && !item.bool;
});
如果查看polyfill,您会注意到循环:
if (!Array.prototype.filter) {
Array.prototype.filter = function(fun/*, thisArg*/) {
'use strict';
if (this === void 0 || this === null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function') {
throw new TypeError();
}
var res = [];
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) { // <--
if (i in t) {
var val = t[i];
// NOTE: Technically this should Object.defineProperty at
// the next index, as push can be affected by
// properties on Object.prototype and Array.prototype.
// But that method's new, and collisions should be
// rare, so use the more-compatible alternative.
if (fun.call(thisArg, val, i, t)) {
res.push(val);
}
}
}
return res;
};
}
if(!Array.prototype.filter){
Array.prototype.filter=函数(fun/*,thisArg*/){
"严格使用",;
if(this==void 0 | | this==null){
抛出新的TypeError();
}
var t=对象(本);
var len=t.length>>>0;
如果(乐趣的类型!=“功能”){
抛出新的TypeError();
}
var-res=[];
var thisArg=arguments.length>=2?参数[1]:无效0;
对于(var i=0;i