Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

javascript - 如何根据属性过滤对象数组?(How to filter object array based on attributes?)

I have the following JavaScript array of real estate home objects:(我有以下JavaScript数组的房地产主页对象:)

var json = { 'homes': [{ "home_id": "1", "price": "925", "sqft": "1100", "num_of_beds": "2", "num_of_baths": "2.0", }, { "home_id": "2", "price": "1425", "sqft": "1900", "num_of_beds": "4", "num_of_baths": "2.5", }, // ... (more homes) ... ] } var xmlhttp = eval('(' + json + ')'); homes = xmlhttp.homes; What I would like to do is be able to perform a filter on the object to return a subset of "home" objects.(我想做的是能够对对象执行过滤,以返回“家庭”对象的子集。) For example, I want to be able to filter based on: price , sqft , num_of_beds , and num_of_baths .(例如,我希望能够基于: pricesqftnum_of_bedsnum_of_baths 。) How can I perform something in JavaScript like the pseudo-code below:(我该如何在JavaScript中执行类似下面的伪代码的操作:) var newArray = homes.filter( price <= 1000 & sqft >= 500 & num_of_beds >=2 & num_of_baths >= 2.5 ); Note, the syntax does not have to be exactly like above.(注意,语法不必与上面完全相同。) This is just an example.(这只是一个例子。)   ask by JGreig translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the Array.prototype.filter method:(您可以使用Array.prototype.filter方法:)

var newArray = homes.filter(function (el) { return el.price <= 1000 && el.sqft >= 500 && el.num_of_beds >=2 && el.num_of_baths >= 2.5; }); Live Example:(现场示例:) var obj = { 'homes': [{ "home_id": "1", "price": "925", "sqft": "1100", "num_of_beds": "2", "num_of_baths": "2.0", }, { "home_id": "2", "price": "1425", "sqft": "1900", "num_of_beds": "4", "num_of_baths": "2.5", }, // ... (more homes) ... ] }; // (Note that because `price` and such are given as strings in your object, // the below relies on the fact that <= and >= with a string and number // will coerce the string to a number before comparing.) var newArray = obj.homes.filter(function (el) { return el.price <= 1000 && el.sqft >= 500 && el.num_of_beds >= 2 && el.num_of_baths >= 1.5; // Changed this so a home would match }); console.log(newArray); This method is part of the new ECMAScript 5th Edition standard, and can be found on almost all modern browsers.(此方法是新ECMAScript 5th Edition标准的一部分,几乎可以在所有现代浏览器中找到。) For IE, you can include the following method for compatibility:(对于IE,可以包括以下兼容性方法:) if (!Array.prototype.filter) { Array.prototype.filter = function(fun /*, thisp*/) { var len = this.length >>> 0; if (typeof fun != "function") throw new TypeError(); var res = []; var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) { var val = this[i]; if (fun.call(thisp, val, i, this)) res.push(val); } } return res; }; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...