Skip to content

Instantly share code, notes, and snippets.

@vickey-dev
Last active December 13, 2021 17:41
Show Gist options
  • Save vickey-dev/3b48b8350c9f866753c6258b74a93cf0 to your computer and use it in GitHub Desktop.
Save vickey-dev/3b48b8350c9f866753c6258b74a93cf0 to your computer and use it in GitHub Desktop.
/**
* @desc consecutiveRoleIdsCountListWithIndex:
* -> will check if each element is the same as the one before it in the array
* -> if yes it will skip that id & increment the repeated count by +1,
* -> it will preserve the non-consecutive duplicates also
* output : [{"roleId":"5fdf1c022a4b9dea","repeatedCount":2,"index":0},{"roleId":"cb1c0e2f-aabd-303b-8db4-94458cf0c3ca","repeatedCount":1,"index":2},{"roleId":"5fdf1c022a4b9dea","repeatedCount":1,"index":3},{"roleId":"da0384e0-3e89-3df9-9f7f-191e5766d36b","repeatedCount":1,"index":4}]
* @param arrayList
* @returns []
*/
function consecutiveRoleIdsCountListWithIndex(arrayList) {
let roles = [];
for (let i = 0; i < arrayList.length; i++) {
let obj = { roleId: null, repeatedCount: 1, index: '' };
if (i === 0) {
obj.roleId = arrayList[i]['roleId'];
obj.index = i
roles.push(obj);
} else {
if (arrayList[i]['roleId'] !== arrayList[i - 1]['roleId']) {
obj.roleId = arrayList[i]['roleId'];
obj.index = i;
roles.push(obj);
} else {
let arrLen = roles.length;
roles[arrLen - 1].repeatedCount = roles[arrLen - 1].repeatedCount + 1;
}
}
}
return roles;
}
//https://stackoverflow.com/a/49144603/9082543
function deleteNth(arr, x) {
var cache = {};
return arr.filter(function (n) {
cache[n] = (cache[n] || 0) + 1;
return cache[n] <= x;
});
}
let s = deleteNth([1, 1, 3, 3, 7, 2, 2, 2, 2], 2);
console.log(s);
const array1 = [ {id:1},{id:2},{id:3} ];
const array2 = [ {id:3} ]
//need to get the object list which are not present in the array2 against array1
let result = array1.filter(a1 => !array2.find(a2 => a2.id === a1.id));
console.log(result)
//[ { id: 1 }, { id: 2 } ]
//for array of strings check
//https://stackoverflow.com/a/33034768
//grouping-elements-in-array-by-multiple-properties
//link: https://codereview.stackexchange.com/questions/37028/grouping-elements-in-array-by-multiple-properties
function groupBy( array , f )
{
var groups = {};
array.forEach( function( o )
{
var group = JSON.stringify( f(o) );
groups[group] = groups[group] || [];
groups[group].push( o );
});
return Object.keys(groups).map( function( group )
{
return groups[group];
})
}
var result = groupBy(list, function(item)
{
return [item.lastname, item.age];
});
let toRemove = [1,2,3];
let targetArray = [1,2,4,5,6];
targetArray.filter(el=>!toRemove.includes(el));
//[ 4, 5, 6 ]
//https://stackoverflow.com/a/30716969/9082543
let a = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4, 5, 5, 5];
let b = a.filter(function(item, pos, arr){
// Always keep the 0th element as there is nothing before it
// Then check if each element is different than the one before it
return pos === 0 || item !== arr[pos-1];
});
console.log(b)
//output: [ 5, 2, 9, 4, 5 ]
let reusableFilter = (obj, key) => obj.filter(v => v[key] != null);
reusableFilter(arrayObjectList,keyAttribute)
//https://gist.github.com/ecarter/1423674
/**
* Sort array of objects based on another array
*/
function mapOrder (array, order, key) {
array.sort( function (a, b) {
var A = a[key], B = b[key];
if (order.indexOf(A) > order.indexOf(B)) {
return 1;
} else {
return -1;
}
});
return array;
};
/**
* Example:
*/
var item_array, item_order, ordered_array;
item_array = [
{ id: 2, label: 'Two' }
, { id: 3, label: 'Three' }
, { id: 5, label: 'Five' }
, { id: 4, label: 'Four' }
, { id: 1, label: 'One'}
];
item_order = [1,2,3,4,5];
ordered_array = mapOrder(item_array, item_order, 'id');
//given object will be sorted by the keys
Object.keys(objectInfo).sort().reduce(function (acc, key) {
acc[key] = objectInfo[key];
return acc;
}, {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment