<script type="text/javascript">
(function test(){
var testJson = [{name : "이건", salary : 50000000},
{name : "홍길동", salary : 1000000},
{name : "임신구", salary : 3000000},
{name : "이승룡", salary : 2000000}];
var newJson = testJson.filter(function(element){
console.log(element);
return element.name == "이건";
});
console.log("newObj");
console.log(newJson);
})();
</script>
json 데이터의 유효성 검증에도 활용
<script type="text/javascript">
(function test(){
var testJson = [{name : "이건", salary : 50000000},
{name : "홍길동", salary : 1000000},
{name : "임신구", salary : 3000000},
{name : "이승룡", salary : 2000000},
{},
{name : "이철수", salary : NaN},
{name : "이재춘", salary : 'undefined'},
{name : "이제일", salary : -2000000}];
function numberFilter(obj){
if ('salary' in obj && (typeof(obj.salary)==='number') && !isNaN(obj.salary) && obj.salary>0){
return true;
}else{
return false;
}
}
var newJson = testJson.filter(numberFilter);
console.log("newObj");
console.log(newJson);
})();
</script>
댓글 없음:
댓글 쓰기