0

I have a list as below:

var myList = [{type:'Prospect__c', typeName__c:'high_school', },
{type:'Procedure__c', typeName__c:'in_program', },
{type:'Procedure__c', typeName__c:'attention_plz', }]

I want to check if in_program (which is present in my list), exists in my list or not? I've used different types of codes in order to get the result but I did not succeed.

Here are some of my tries:

if (myList.indexOf('in_program')>0)

if (myList.indexOf(typeName__c:'in_program')>0)

if (myList.indexOf(" typeName__c:'in_program' ")>0)

Community
  • 1
  • 1
ReshaD
  • 936
  • 2
  • 18
  • 30

5 Answers5

2

There are a bunch of different strategies you can follow, but indexOf would only work if you were looking for the exact same root object, not one of its properties

A good approach would be with filter:

var filteredItems = myList.filter(function(a) { 
  return a.typeName__c === 'in_program' 
})
console.log(filteredItems.length === 0) // false

This will return a new collection with the items filtered by the returned value of the callback. Then, you check the length and see if it's 0 or greater.

Eduardo Páez Rubio
  • 1,032
  • 2
  • 9
  • 31
2

You can use Array.some:

var isInList = myList.some(function (obj) {
    return 'in_program' === obj.typeName__c;
});
Hyddan
  • 1,297
  • 15
  • 24
2

You can use following approach in different scenario:

JSON.stringify

If its just about finding availability, you can get JSON string and then check in it.

As correctly pointed by corn3lius, just checking search value will search in keys as well. You can wrap searchValue in ":..." and this will only search in values

var myList = [{type:'Prospect__c', typeName__c:'high_school', },
{type:'Procedure__c', typeName__c:'in_program', },
{type:'Procedure__c', typeName__c:'attention_plz', }]
var searchVal = 'in_program';

var exist= JSON.stringify(myList).indexOf(":\"" + searchVal + "\"") > -1;

console.log(exist)

Array.some

An alternate could be using array function if you know the exact key to lookup

var myList = [{type:'Prospect__c', typeName__c:'high_school', },
{type:'Procedure__c', typeName__c:'in_program', },
{type:'Procedure__c', typeName__c:'attention_plz', }]
var searchVal = 'in_program';

var exist= myList.some(function(o){ return o.typeName__c === searchVal });

console.log(exist)

Array.find

If you want to find first object where value matches, you should use Array.find

var myList = [{type:'Prospect__c', typeName__c:'high_school', },
{type:'Procedure__c', typeName__c:'in_program', },
{type:'Procedure__c', typeName__c:'attention_plz', }]
var searchVal = 'in_program';

var exist= myList.find(function(o){ return o.typeName__c === searchVal });

console.log(exist)

Array.filter

If you want to find all objects where value matches, you should use Array.filter

var myList = [{type:'Prospect__c', typeName__c:'high_school', },
{type:'Procedure__c', typeName__c:'in_program', },
{type:'Procedure__c', typeName__c:'attention_plz', }]
var searchVal = 'Procedure__c';

var exist= myList.filter(function(o){ return o.type === searchVal });

console.log(exist)
Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
1

Check the code below:

var myList = [{type:'Prospect__c', typeName__c:'high_school', },
{type:'Procedure__c', typeName__c:'in_program', },
{type:'Procedure__c', typeName__c:'attention_plz', }];

var length = myList.length;

for(i = 0; i < length ; i++){

    if(myList[i].typeName__c == 'in_program')
  {
    alert("true");
  }
}
selvakumar
  • 634
  • 7
  • 16
1
var entry = mylist.find(function(e){ return e.typeName__c === 'in_program'; });
if( entry ) {
   // found object in list 
}

documented here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

corn3lius
  • 4,857
  • 2
  • 31
  • 36