Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

filter array with unique objects javascript

const array =
  [
    { "name": "Joe", "age": 17 },
    { "name": "Bob", "age": 17 },
    { "name": "Carl", "age": 35 }
  ]

const key = 'age';

const arrayUniqueByKey = [...new Map(array.map(item =>
  [item[key], item])).values()];

console.log(arrayUniqueByKey);

   /*OUTPUT
       [
        { "name": "Bob", "age": 17 },
        { "name": "Carl", "age": 35 }
       ]
   */

 // Note: this will pick the last duplicated item in the list.
Comment

PREVIOUS NEXT
Code Example
Javascript :: strike react native 
Javascript :: js for each element class 
Javascript :: react native rotate image 
Javascript :: javascript angle equation of a line 
Javascript :: Render static html files in express 
Javascript :: js new line regex 
Javascript :: python request text to dict 
Javascript :: Get First Day and last day of week javascript 
Javascript :: iframe getelementbyid 
Javascript :: javascript remove parentheses 
Javascript :: how to remove a class in js after 100 milliseconds 
Javascript :: javascript isset 
Javascript :: javascript change attribute 
Javascript :: × MUI: makeStyles is not longer exported from @mui/material/styles. You have to import it from @mui/styles. 
Javascript :: javascript hass class 
Javascript :: ReactDOM.render is no longer supported in React 18. Use createRoot instead 
Javascript :: javascript link to go back 
Javascript :: js range 1 to n 
Javascript :: sequelize undo last migration 
Javascript :: uuid generator pure javascript 
Javascript :: disable yellow box react native 
Javascript :: javascript remove space from two side of string 
Javascript :: datatable remove show 
Javascript :: Javascript detect mobile browser 
Javascript :: jest timeout 
Javascript :: after load page jquery 
Javascript :: detect if scrolled to bottom 
Javascript :: pass number as a prop in react 
Javascript :: js sync delay 
Javascript :: has key js 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =