In our example, we use it to check if the current element in array A is present in array B. The some() method returns true if at least one element in the array passes the test implemented by the provided function. If an element is not present in array B, then we add it to an accumulator array: const A = N the example below, we use the reduce() method to iterate over each element in array A and check if it is present in array B using the some() method. See also: Checking if an array contains a given object in JavaScript Using the reduce() and some() method By combining these two methods, we can easily find elements that are present in array A but not in array B.Ĭonst result = A.filter(x => !B.includes(x)) The filter() method is used to filter out elements from an array based on a condition while the includes() method is used to check whether an element exists in an array or not. Get an array of dates between 2 given datesĤ Using the Set object and the filter() method Using the filter() and includes() methods Sorting an Array of Objects by Date PropertyĬreate an Object from Arrays of Keys and Values Truncate the time portion of a date string Get current date time in yyyy/MM/dd HH:mm:ss formatĢ Ways to Convert a String into a Date ObjectĬonvert Timestamp to Date Time and Vice Versa Pass an Array to a Function as multiple Arguments Remove elements that occur in one array from another Generate an array of random elements with a given lengthĬheck if an array contains a given objectįind elements in array A but not in array B Using Arrow Functions in JavaScript ClassesĬount the occurrences of elements in an array Generate a random number between Min and Max and exclude a specific numberĬalculate Variance and Standard Deviation Remove leading and trailing whitespace from a stringĬheck if a String Starts or Ends With a Substringĭifferent Ways to Generate Random StringsĬonvert a byte array to a hex string and vice versa JavaScript Regular Expressions Cheat SheetĬount the occurrences of each word in a string
0 Comments
Leave a Reply. |