ReshaD
ReshaD

Reputation: 946

Check if a string is in a list or not (JavaScript)

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)

Upvotes: 0

Views: 293

Answers (5)

Rajesh
Rajesh

Reputation: 24945

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)

Upvotes: 2

corn3lius
corn3lius

Reputation: 4985

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

Upvotes: 1

Hyddan
Hyddan

Reputation: 1337

You can use Array.some:

var isInList = myList.some(function (obj) {
    return 'in_program' === obj.typeName__c;
});

Upvotes: 2

Eduardo Páez Rubio
Eduardo Páez Rubio

Reputation: 1152

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.

Upvotes: 2

selvakumar
selvakumar

Reputation: 654

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");
  }
}

Upvotes: 1

Related Questions