javascript/angularjs looping through an array show only one object -
in angularjs modal trying display classes have level 4 got one. please how should alter in order class
$scope.openclass = function (classes) { $log.info("classes",classes); var modalinstance = $modal.open({ templateurl: 'classes.html', controller: 'modalclassinstancectrl', resolve: { info: function () { var info = {}; (var i=0;i<classes.length;i++){ if (classes[i].level==4){ info['name']= classes[i].name; $log.info("classinfo",info); } } $log.info(info); return info; } } });
the $log in if condition show me write classes 2 in case $log after loop show me one
you use filter method, following:
var filteredarray = classes.filter(function(value) { return value.level == 4; ));
after it, if want pluck name of items can following:
var names = filteredarray.map(function(obj) { return obj.name; }
if going often, write function abstracts away map, way:
function pluck(array, key) { return array.map(function(item) { return item[key]; }); }
Comments
Post a Comment