2

I have the following :

$scope.Items = [{"ItemId": 1, "ItemName":"Test1"}, {"ItemId": 2, "ItemName":"Test2"}, {"ItemId": 3, "ItemName":"Test3"}];

If I wish to check if ItemId 1 AND ItemId 2 exist within this, what would be the best approach for it? I believe the indexOf property should work, but how can it be used to check if a particular attribute value of the object exists within the array?

3
  • Try Array.find : developer.mozilla.org/en/docs/Web/JavaScript/Reference/… Commented Oct 27, 2016 at 3:05
  • Any idea how I could add the value of "ItemId" within the find() function? Commented Oct 27, 2016 at 5:13
  • Please refer to my answer below. Commented Oct 27, 2016 at 5:23

4 Answers 4

3

you can use like also

$scope.firstitem=false;
$scope.seconditem=false;
angular.forEach($scope.Items, function (value, key) {
            if (value.ItemId== 1) { $scope.firstitem=true; }
            if (value.ItemId== 2) { $scope.seconditem=true; }
        });
Sign up to request clarification or add additional context in comments.

Comments

3

You could use find function like this to find by property you want.

var inventory = [
    {name: 'apples', quantity: 2},
    {name: 'bananas', quantity: 0},
    {name: 'cherries', quantity: 5}
];

function findCherries(fruit) { 
    return fruit.name === 'cherries';
}

console.log(inventory.find(findCherries)); 
// { name: 'cherries', quantity: 5 }

If you wish to filter (i.e , find multiple values.) you can use the filter function like this:

var arr = [
  { id: 15 },
  { id: -1 },
  { id: 0 },
  { id: 3 },
  { id: 12.2 },
  { },
  { id: null },
  { id: NaN },
  { id: 'undefined' }
];

var invalidEntries = 0;

function filterByID(obj) {
  if (obj.id !== undefined && typeof(obj.id) === 'number' && !isNaN(obj.id)) {
    return true;
  } else {
    invalidEntries++;
    return false;
  }
}

var arrByID = arr.filter(filterByID);

console.log('Filtered Array\n', arrByID); 
// Filtered Array
// [{ id: 15 }, { id: -1 }, { id: 0 }, { id: 3 }, { id: 12.2 }]

console.log('Number of Invalid Entries = ', invalidEntries); 
// Number of Invalid Entries = 4

Ref : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Comments

2

You could use lodash's _.some() operator

var _ = require('lodash');
var Items = [{"ItemId": 1, "ItemName":"Test1"}, {"ItemId": 2, "ItemName":"Test2"}, {"ItemId": 3, "ItemName":"Test3"}];

var hasItem1 = _.some(Items,  ["ItemId", 1]);
var hasItem2 = _.some(Items,  ["ItemId", 2]);

console.log ("hasItem1 = "+hasItem1);
console.log ("hasItem2 = "+hasItem2);

Comments

2

You could use lodash's _.findIndex() operator

var _ = require('lodash');
var Items = [{"ItemId": 1, "ItemName":"Test1"}, {"ItemId": 2, "ItemName":"Test2"}, {"ItemId": 3, "ItemName":"Test3"}];

var hasItem1 = false;
var hasItem2 = false;
if(_.findIndex(Items, {ItemId : 1}) != -1) { hasItem1 = true; }
if(_.findIndex(Items, {ItemId : 2}) != -1) { hasItem2 = true; }

console.log ("hasItem1 = "+hasItem1);
console.log ("hasItem2 = "+hasItem2);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.