json data filtering
by abubelinha
HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
</body>
</html>
JavaScript
// noprotect
document_write = function (str){ document.body.insertAdjacentHTML("beforeend", str); }
// http://stackoverflow.com/a/23721038/710788
var jsondata=[{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}];
var as=$(jsondata).filter(function (i,n){return n.website==='yahoo'});
document_write("<h5>with jquery</h5>");
for (var i=0;i<as.length;i++)
{
document_write(as[i].name +" "+as[i].website+"<br>")
}
// http://stackoverflow.com/questions/9483695/filtering-json-data/9483775#9483775
document_write("<h5>javascript</h5>");
var campo='website';
var valor='google';
var filteredJson = jsondata.filter(function (row) {
if(row[campo] ===valor) {
return true
} else {
return false;
}
});
as=filteredJson;
for (var i=0;i<as.length;i++)
{
document_write(as[i].name +" "+as[i].website+'<br>');
}
// https://raw.githubusercontent.com/hugoware/jlinq-beta/master/jlinq.js