IF statement within a find() function?

I have a Test that uses a Find function to pull out an object from an array of objects that matches a set of specific parameters. Example:

let object = response.items.find(x => { 
    x.name = A &&
    x.size = B &&
    x.type = C &&
    x.price = D
  }
);

The test works fine as-is. However, I use this Test with multiple environments. Some of them donā€™t return the JSON response with the exact same format. For example, some of them might leave out ā€œx.priceā€, but include the rest of the key:values. This causes the test to abruptly fail.

Is it possible to include an IF statement within the find() function so that if Iā€™m using a specific environment, it can include/exclude a search parameter?

@SKSG I am pretty sure the code that you shared doesnā€™t work since it has invalid javascript code.
And I am not quite sure youā€™re trying to do achieve with find?

You can read more about Array.find here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Also, yes you can use anything inside the function.

Sample:

let object = response.items.find(x => { 
    if (x.name === 'A') {
		return true;
    }
	else {
		return false;
    }
  }
);

I havenā€™t had luck putting the if statement inside the find function, since itā€™s going to return an object (forgot to add the return statement at the top line within the find function.) Since its looking for a number of matching values, I think it looks at the code within the ā€˜ifā€™ statement as a command, rather than a value to compare to. So far it looks like this:

let object = response.items.find(x => { 
    return (x.name = A) &&
    (x.size = B) &&
    (x.type = C) &&
    if (environment == "foo"){
    (x.price = D)
    }
  }
);

I canā€™t run this properly though, since Postman throws a bunch of errors about including that ā€˜ifā€™ statement. Any other ideas I can work with?

Hey @SKSG. Not sure if I understand the use case entirely but if youā€™re trying to change the keys to consider in a find operation based on some condition, you could do that by moving the condition out of the find block and based on that decide what all to consider.

So for example,

let items = [
    {
        name: 'A',
        size: 'B',
        type: 'C',
        price: 'E'
    },
    {
        name: 'A',
        size: 'B',
        type: 'C',
        price: 'D'
    }
]

let env = 'foo',
    keysToMatch = env === 'foo' ? {
        name: 'A',
        size: 'B',
        type: 'C',
        price: 'D'
    } : {
        name: 'A',
        size: 'B',
        type: 'C',
    },
    object = items.find((item) => {
        Object.keys((key) => {
           if (keysToMatch[key] !== item[key]) {
               return false;
           } 
        });
        return true;
    });
    
console.log(object);

In this case, I moved the keysToMatch to a ternary outside, which decides what all I will consider in the actual .find block.

Please let me know if this was not your question and Iā€™d try to update my answer accordingly.