/    Sign up×
Community /Pin to ProfileBookmark

How to filter object array based on attributes?

Can this be done with Javascript? How difficult is it? i basically want filter for depending on the different types of properties. e.g detached/semi detached/flats/apartments. how would i go about doing this? any help is appreciated.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@svidgenMar 10.2012 — You can certainly filter an array on the client-side by looping through it, same as you would in server-side code. Or, you can filter at the database level with a WHERE clause, assuming your data comes, finally, from a SQL database.

Where the best place to apply the filter is depends on how much of the whole set needs to be used by the client during their stay on the page. You could, for instance, send the client a list of properties filtered (with SQL) by zip code (or whatever) and perform the rest of the filtering on-page for some lightning fast user interaction within the zip code (or whatever).

server side ... assuming PHP:
[code=php]$query = "select id, street, city, state, zip, type, cost from properties where zip={$zip}";
$result = mysql_query($query);
$rv = array();
foreach ($row = mysql_fetch_assoc($result)) {
$rv[] = $row;
}
$properties_json = json_encode($rv);[/code]


What you do with the resulting properties JSON depends, but it's probably either inserted directly on the page or in the body of an asynchronous request. For now, let's assume the JSON string has been interpolated directly in the page in place of {$properties_json} below.

[code=html]var properties = {$properties_json};

// very simple getter method that uses a single key-value pair.
var selectProperties(k, v) {
var rv = [];
for (var i in properties) {
if (properties[i][k] == v) {
rv.push(properties[i]);
}
}
return rv;
} // selectProperties()[/code]
Copy linkTweet thisAlerts:
@usman07authorMar 10.2012 — Thank you very much for the response, i am new to javascript, but how would i do all this with just client side? is that possible?
Copy linkTweet thisAlerts:
@svidgenMar 10.2012 — Well ... if you're pulling static files for your data, you could do it "all" client side. But, that's generally a terrible idea. You won't be able to edit the "database" without some server side code. You'll have to compile your data files using software on your machine (even if it's just notepad) and upload it manually. And you'll have to be very thoughtful about how you organize your datafiles server-side, so you don't have to transmit everything to the client up front.

Though ... I don't know how much data you're dealing with. Maybe sending it all to the client up front isn't an issue.
×

Success!

Help @usman07 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...