0

I need one help.I have an array which contains the json data.I need to check any given key is exist or not in that array using Javascript/Angular.js.I am explaining an array below.

var arr = [{
    'category': 1,
    'subcategory': 2,
    'check': true
}, {
    'category': 1,
    'subcategory': 2
}];

You can check above for 1 index there is no check key.I need to check that key so that i can fetch check:true or check is not present at all.Please help me.

1
  • 2
    object.check ? object.check : '404: not found' Commented May 26, 2016 at 10:13

3 Answers 3

3

You can use some() and hasOwnProperty()

some() executes the callback function once for each element present in the array until it finds one where callback returns a truthy value (a value that becomes true when converted to a Boolean). If such an element is found, some() immediately returns true. Otherwise, some() returns false. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values. (Taken from here)

var arr = [{
  'category': 1,
  'subcategory': 2,
  'check': true
}, {
  'category': 1,
  'subcategory': 2
}];

var key='check';

var res = arr.some(function(v) {
  return v.hasOwnProperty(key);
});

console.log(res);


For older browser check polyfill option of some method.


If you want to get the value or the object which has the property then use find() instead

var arr = [{
  'category': 1,
  'subcategory': 2,
  'check': true
}, {
  'category': 1,
  'subcategory': 2
}];

var key = 'check';

var res = arr.find(function(v) {
  return v.hasOwnProperty(key);
});

console.log(res);


For older browser check polyfill option of find method.


Or with simple for loop

var arr = [{
  'category': 1,
  'subcategory': 2,
  'check': true
}, {
  'category': 1,
  'subcategory': 2
}];
var chk = 0;

for (var i = 0; i < arr.length; i++) {
  if (arr[i].check == true) {
    chk = 1;
    break;
  }
}

console.log(chk);

Sign up to request clarification or add additional context in comments.

4 Comments

@Pranav C Balan :How can i check this inside for loop.Suppose i need to check if check==true{chk=1} if chcek key is not there chk=0.
@Tushar : if he wants the object find() can be used , thanks and answer updated :)
@subhra : only if check==true??
0

As @PranavCBalan said you can use the method hasOwnProperty().

I recommend you to take a look at Lodash. You can find there a lot of utilities that solve the typical checks or functions that you may need in your code.

Comments

0

try this

 angular.forEach(arr,function(value,key){
      if(value.check == true)
        value.check = 1;
       else if(value.check == undefined)
             value.check = 0;
   })

var app = angular
  .module('MyApp', [
  ])
.controller('Main', ['$scope', function ($scope) { 
  
      $scope.arr = [
                {
                  'category': 1,
                  'subcategory': 2,
                  'check': true
                },
                {
                  'category': 1,
                  'subcategory': 2
                }
              ];
      angular.forEach($scope.arr,function(value,key){
      if(value.check == true)
        value.check = 1;
       else if(value.check == undefined)
             value.check = 0;
       })
}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 <div class="main-content" ng-app="MyApp" ng-controller="Main">
     <pre>{{arr|json}}</pre>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.