我有这样的数组
var arr = [{a:1, ...},{a:1, ...},{c:2, ...},{a:3, ...},{a:1, ...}]
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到数组的长度arr[x].a != 1.
我知道这可以使用for loop,push但我希望遵循每个体面的程序员应该做的最佳实践.
PS FYI我正在使用AngularJS.
编辑1
对于任何因为ES6脚本而arrow function在sajeetharan的答案中遇到问题的人都是解决方案
arr.filter(function(a){return a.a != 1}).length;
Run Code Online (Sandbox Code Playgroud) 我想知道$ scope的双重行为.在下面的脚本中,我获得了name警报的价值.但在我的离子应用程序中,相同的代码提醒undefined.
我搜索了这个问题并发现这个链接作为一个解决方案,它表明我们需要使用点(.)来获取值ng-model.两者有什么区别.
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.a =function a(){alert($scope.name);}
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="name" ng-blur="a()">
</div>Run Code Online (Sandbox Code Playgroud)
我对以下代码的期望是它会绑定$ scope.as.但没有任何显示,控制台中没有显示错误
var testModule = angular.module('testmodule', [])
.controller('ctrl', function ($scope, mser) {
mser.aa();
})
.service('mser', function($scope /* Would $scope be here */) {
this.aa = function(){
$scope.as = "hello mars"
}});
Run Code Online (Sandbox Code Playgroud) 我有一个具有以下结构的JSON对象
{"30-31":[{"msg":"hello","usr":"31"}],
"33-30":[{"msg":"shah hi","usr":"30"}]}
Run Code Online (Sandbox Code Playgroud)
我可以执行什么操作来获取这样的数组["30-31", "33-30"].我尽力使用.map等但没有成功,有人可以帮忙吗?
我有以下来自 API 的回复
[{name : Taj Mahal, lat : 111 , lng : 222},
{name : Agra Fort, lat : 333 , lng : 444},
{name : Red Fort, lat : 555 , lng : 666}]
Run Code Online (Sandbox Code Playgroud)
我希望填充这样的数组
var coordinates = [[Taj Mahal, 111, 222],
[Agra Fort, 333, 444],
[Red Fort, 555, 666]]
Run Code Online (Sandbox Code Playgroud)