The expression […new Set(recordIdArray)] in JavaScript provides a simple and efficient way to remove duplicates from an array. The Set object inherently stores only unique values, so when you passrecordIdArray to new Set(), all duplicate elements are automatically filtered out. The spread operator … then converts the set back into a regular array, preserving the… Continue reading Remove duplicates from an array
Tag: Array
Object with key value pair to Array
const RECEPIENTS = { ‘chris’: 21661, ‘sam’: 21628, ‘danna’: 21629, ‘anil’: 3, ‘danny’: 21633, ‘ahap’: ‘aha-shipping@airportapplaincehelp.zendesk.com’, ‘fernanda’: 86081, ‘laura’: ‘laura.martinez@airportappliance.com’, … Continue reading Object with key value pair to Array
Sort Array of strings in javascript
The following function can be used to sort an array of strings in descending order. /** * Sort array in descending order * @param {*} arr array to be sorted * @returns sorted array */ function sortDescending(arr) { arr.sort((a,… Continue reading Sort Array of strings in javascript
The function to adds a value to the first index of an array
/** * This function adds a value to the first index of an array. * @param {Array} arr – The array to which the value will be added. * @param {*} value – The value to add to the array. * @returns {Array} The modified… Continue reading The function to adds a value to the first index of an array
JavaScript To Find the most frequent item of an array
If we want to filter the item from the array which is frequently used then we can follow the below Example Here in the below example the array jjarray contain ‘a’ 5 times , so the most frequently item will be a. Sample array: var jjarray=[3, ‘a’, ‘a’, ‘a’, 2, 3, ‘a’, 3, ‘a’, 2,… Continue reading JavaScript To Find the most frequent item of an array
Convert array into key value pair
JavaScript Array Functions
Map, reduce, and filter are all array methods in JavaScript. Each one will iterate over an array and perform a transformation or computation. Each will return a new array based on the result of the function. Map The map() method is used to create a new array from an existing one, applying a function to each… Continue reading JavaScript Array Functions
Array Merge/ Combine in JS
Merge without removing duplicate elements -> Using concat() Method: The concat() method accepts arrays as arguments and returns the merged array. // with elements of nums1 and returns the // combined array – combinedSum arraylet combinedNums = nums1.concat(nums2, nums3);// More readable formlet combinedNums = [].concat(nums1, nums2, nums3); -> Using spread operator: Spread operator spreads the value of the array into… Continue reading Array Merge/ Combine in JS
Remove array values
To remove values from array in js use _.reject on array. From array ‘categoryArray’ remove values equal to variable ‘hideCategory’.var categoryArray = originalRet.categories[1].categories;categoryArray= _.reject(categoryArray, function (method) { return method.text==hideCategory; });