Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

lodash uniqBy alterantive in js

const uniqBy = (arr, predicate) => {
  const cb = typeof predicate === 'function' ? predicate : (o) => o[predicate];
  
  return [...arr.reduce((map, item) => {
    const key = (item === null || item === undefined) ? 
      item : cb(item);
    
    map.has(key) || map.set(key, item);
    
    return map;
  }, new Map()).values()];
};

const sourceArray = [ 
  { id: 1, name: 'bob' },
  { id: 1, name: 'bill' },
  null,
  { id: 1, name: 'bill' } ,
  { id: 2,name: 'silly'},
  { id: 2,name: 'billy'},
  null,
  undefined
];

console.log('id string: ', uniqBy(sourceArray, 'id'));

console.log('name func: ', uniqBy(sourceArray, (o) => o.name));
Comment

PREVIOUS NEXT
Code Example
Javascript :: xmlhttprequest set route params 
Javascript :: { "rules": { ".read": true, ".write": true } } 
Javascript :: mutiple if in express handlebars 
Javascript :: how to get value from select option using input name in jquery 
Javascript :: js console 
Javascript :: dev console with colored font 
Javascript :: html and js integrate 
Javascript :: state changes when changing route useContext next 
Javascript :: contoh penggunaan promise 
Javascript :: element vs node 
Javascript :: solana solana-Web3.js change for devnet lamports to production transaction 
Javascript :: cypher neo4j 
Javascript :: dynamic components 
Javascript :: jquery: return true or false if the element is present in the DOM or not 
Javascript :: convert milliseconds to hours minutes seconds javascript 
Javascript :: const { message } 
Javascript :: javascript array filter exercises 
Javascript :: React Hook "useState" is called in function "cardState" which is neither a React function component or a custom React Hook function 
Javascript :: add multiple parameters js 
Javascript :: my env.local file not working in my react app usind mac 
Javascript :: logvalue returned from array 
Javascript :: babel 7 ReferenceError: regeneratorRuntime 
Javascript :: duplicate serial numbers asset 
Javascript :: staticDir storybook svg and images not loading 
Javascript :: how to add datepicker in appended input 
Javascript :: regular expression for spanish date 
Javascript :: loopover iterate elements by name js 
Javascript :: check if element is displayed 
Javascript :: how to make local storage read only site:stackoverflow.com 
Javascript :: simple form in react native with code 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =