Thursday 8 October 2020

Generate data format from current date to x months

 /**

 * Generate data format from current date to x months
*
* @param $months
*
* @return DateTime|string
*
* @throws Exception
*/
function generateDateByMonths($months)
{
$date = '';
$time = '23:59:59';
if(is_numeric($months) && $months > 0) {
$date = new DateTime('now');
$date->modify('+'.$months.' month'); // or you can use '-2 months' for deduct
$date = $date->format('Y-m-d');
}
return $date . ' ' . $time;
}

Tuesday 3 March 2020

JavaScript: remove duplicate in multidimensional array by using specific key

     function removeDuplicatesMulti(array) {
// Declare a new array
var newArray = [];
 
// Declare an empty object
var uniqueObject = {};
 
// Loop for the array elements
for (var i in array) {
// Extract the title or key based on duplicate finding
objTitle = array[i]['ID'];
 
// Use the title as the index
uniqueObject[objTitle] = netSerices[i];
}
 
// Loop to push unique object into array
for (i in uniqueObject) {
newArray.push(uniqueObject[i]);
}

return newArray;
}

JavaScript finding intersection values from the 2 arrays

function arrayIntersection(ar1, ar2) {
return ar1.filter(function(n) {
return ar2.indexOf(n) !== -1;
});
}

JavaScript function to remove duplicates in single dimension array

function removeDuplicatesInSingleDimensionArray(array) {
  return array.filter((a, b) => array.indexOf(a) === b)
};