How to Check if string is in list in javascript

0 votes

I wanted to check if an ad was displayed on an allowed website using javascript. I used a methodology explained in that post Determine if string is in list in JavaScript.

Here is my code :

async function ImpCertification(tx) {

    Impression = await getAssetRegistry('org.example.basic.Impression')

    const source = Impression.PrintedSite

    whitelist = ["Youtube", "Google", "Facebook", "Twitter"]

    if (whitelist.indexOf(source) < 0) {
        // Checks if source in whitelist
        throw new Error ('This impression does not respect branding')

    // Checks every necessary conditions to validate impression
    if (whitelist.indexOf(source) >=0) {
        // Save the old value of the asset.
        const oldValue = Impression.Valid;

        // Update the asset with the new value.
        Impression.Valid = true;

        // Get the asset registry for the asset.
        const assetRegistry = await getAssetRegistry('org.example.basic.transaction.ValidateImpression');
        // Update the asset in the asset registry.
        await assetRegistry.update(Impression);

        // Emit an event for the modified asset.
        let event = getFactory().newEvent('org.example.basic', 'Validation');
        event.asset = tx.asset;
        event.oldValue = oldValue;
        event.newValue = true;
    await null

Can someone tell me how to do this?

Sep 19, 2018 in Blockchain by digger
• 26,740 points

1 answer to this question.

0 votes
At first, make sure that the string that you pass to indexOf is an actual string:

typeof <your-string-variable> === 'string'

To check if a string is in your list I suggest using ES6 Array.prototype.includes() :

['a', 'b', 'c'].includes('b')

To check if a string is a substring of the strings in your list combine Array.prototype.includes()method with String.prototype.includes() method:

(I also used ES6 reduce there to simplify the expression)

['hello', 'world', 'my', 'name', 'is', 'johny', 'cash'].reduce((current, item) => current || item.includes('substring_to_search'), false);

Hope this helps :-)
answered Sep 19, 2018 by slayer
• 29,360 points

Related Questions In Blockchain

+1 vote
3 answers

How efficient is Ethereum in regard to its TPS??

TPS means transactions processing at any given ...READ MORE

answered Apr 16, 2018 in Blockchain by Shashank
• 10,400 points
+1 vote
1 answer

How is it possible to achieve privacy and confidentiality in smart contracts??

Any contract code written on the blockchain ...READ MORE

answered Apr 18, 2018 in Blockchain by Shashank
• 10,400 points

edited Aug 7, 2018 by Omkar 809 views
0 votes
1 answer

In a Blockchain, how difficult is it to modify the third to last block?

Technically, it's not difficult at all, all ...READ MORE

answered Apr 20, 2018 in Blockchain by Christine
• 15,790 points
0 votes
1 answer

How to check the data integrity logic in proof of work mining?

The proof of work is actually works ...READ MORE

answered May 8, 2018 in Blockchain by Johnathon
• 9,090 points
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
0 votes
2 answers

How to check if bitcoin address is valid?

For Java, have a look at the ...READ MORE

answered Aug 20, 2018 in Blockchain by Omkar
• 69,230 points
0 votes
1 answer

How to round a number down to 8 decimal places if its over 8 decimal places in Javascript

Try it in this way: function nrOfDecimals(number) { ...READ MORE

answered Sep 12, 2018 in Blockchain by slayer
• 29,360 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP