The _.reject() function is used to give the answer which does not match with the given condition. It is used to find out those elements from the list which does not satisfy the given condition and then to make changes to them only. When all the elements of the list are passed to the function/iterates and no more elements remains then the _.reject() loop ends. It is opposite of _.filter() function as filter selects those items which satisfy the given condition.
_.reject( list, predicate, [context] )
Parameters: This function accepts three parameters as mentioned above and described below:
- List: This parameter contains the element list.
- Predicate: This parameter contains the condition which is used to reject the elements.
- Context: It is the text which is used to display. It is optional parameter.
Return values: This function returns the array of elements which does not fulfill the condition of _.reject() function.
Passing a list of numbers to _.reject() function: The ._reject() function takes the element from the list one by one and do the operations on the code. Below is the example to find the odd elements from the list.
Passing a structure to the _.reject() function: Choose one property of the items on the basis of which the condition will be applied. Below used the ‘disabled’ property. Since the ‘item2’ doesn’t have the disabled property so it is rejected and given as output.
Passing a list of numbers and a value to match with item: The elements from the array which does not match with the given number are rejected and hence displayed as array in output.
Using two _.reject() function together: Pass different objects to each _.reject() function and then use the following results together by using the logical operators like &&, ||, ! etc. Here, both the 2 objects and the 2 arraylists are rejected as they does not fulfill the given condition so they are given as output.