Get key names if values is true

时间:2015-10-30 23:30:45

标签: arrays key

I need to get only the keys that has the value true, and return me the key name. Question: I need to use the underscore or can not do in a simpler way? {"desenvolvimento": true, "artigos": false, "design": true} RESOLVED: More simple. <span ng-repeat="(key, value) in item.tax.categorias" ng-if="value">{{key}}</span>

1 个答案:

答案 0 :(得分:1)

Simply use a for in loop and test the prop! for(var prop in obj){ if(obj[prop] === true){ // this is true } } I encourage you to read through MDN for all the ways you can interact with objects and arrays https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in