过滤不需要的数据的javascript
我是新手。我有一个链接,我想得到一个名单的名称和id 我可以从json中的链接获取列表,但我需要过滤列表中不需要的项目。如果id超过4位,则删除全名、名称、短名称和id。例如:如果id:123456,则需要将其与名称、短名称一起过滤掉 app.js details.js html过滤不需要的数据的javascript,javascript,angularjs,json,Javascript,Angularjs,Json,我是新手。我有一个链接,我想得到一个名单的名称和id 我可以从json中的链接获取列表,但我需要过滤列表中不需要的项目。如果id超过4位,则删除全名、名称、短名称和id。例如:如果id:123456,则需要将其与名称、短名称一起过滤掉 app.js details.js html 如果您需要过滤4位以上的id值,那么您可以使用简单的条件ifresponse[0][i].id进行限制,您可以使用Array.prototype.filter和Array.prototype.map,这非常优雅 $io
如果您需要过滤4位以上的id值,那么您可以使用简单的条件ifresponse[0][i].id进行限制,您可以使用Array.prototype.filter和Array.prototype.map,这非常优雅
$ionicLoading.show();
if($scope.currentMarket == "abc") {
$webServicesFactory.getNotParsed($marketProvider[$scope.currentMarket].searchRefURL).then(
function success(response) {
$scope.searchRef = JSON.parse(response)[0].filter(function(itm) {
// whatever you want to filter should pass this condition
return itm.id.toString().length <= 3;
}).map(function(itm) {
// for each item, transform to this
return {
name: itm.name || itm.full_name,
symbol: itm.short_name,
code: itm.id,
market: $marketProvider[$scope.currentMarket].long
};
});
$ionicLoading.hide();
}
);
}
确保处理任何错误并使代码具有防御性。您尝试了什么吗?通过基本的Javascript条件检查,您可以很容易地做到这一点。您可以使用Array.prototype.filter和Array.prototype。map@Miro你能详细说明一下答案吗?@Hoyen我想用id的长度,若大于4,就把它去掉,但我不知道怎么写angularjs@bkcollection下面是示例$scope.searchRef=response[0].filteritem=>item.id.LengthRefResponse[0][i].id是,如果id小于或等于999,则只有条件为true,并将对象推送到$scope.searchRef数组变量。所以$scope.searchRef的其余值都在4位以上的id值扫描它是否使用id的长度?请给我一些建议。某些id是“3074LA”,它不是数字RoH,因此您可以在响应[0][i].id.toString.length时使用此条件。这对于性能而言也应该比多次推送到作用域中的数组要好,因为角度摘要循环只触发一次。
$ionicLoading.show();
if ($scope.currentMarket == "abc"){
$webServicesFactory.getNotParsed($marketProvider[$scope.currentMarket].searchRefURL).then(function success(response){
response = JSON.parse(response);
for (var i = 0; i < response[0].length; i++){
$scope.searchRef.push({
name: response[0][i].name || response[0][i].full_name,
symbol: response[0][i].short_name,
code: response[0][i].id,
market: $marketProvider[$scope.currentMarket].long
});
}
console.info($scope.searchRef);
$ionicLoading.hide();
});
}
<div class="list">
<div class="item" ng-repeat="item in searchItems" ng-click="openDetail(item)">
<p>{{item.symbol}} - {{item.name}}</p>
<p>{{currentMarket | uppercase}}</p>
</div>
</div>
for(var i=0; i<response[0].length; i+=1){
if(response[0][i].id.toString().length <= 3 ) {
$scope.searchRef.push(
{
name: response[0][i].name || response[0][i].full_name,
symbol: response[0][i].short_name,
code: response[0][i].id,
market: $marketProvider[$scope.currentMarket].long
}
);
}
}
$ionicLoading.show();
if($scope.currentMarket == "abc") {
$webServicesFactory.getNotParsed($marketProvider[$scope.currentMarket].searchRefURL).then(
function success(response) {
$scope.searchRef = JSON.parse(response)[0].filter(function(itm) {
// whatever you want to filter should pass this condition
return itm.id.toString().length <= 3;
}).map(function(itm) {
// for each item, transform to this
return {
name: itm.name || itm.full_name,
symbol: itm.short_name,
code: itm.id,
market: $marketProvider[$scope.currentMarket].long
};
});
$ionicLoading.hide();
}
);
}