Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

remove object if key is duplicate javascript

let array =   [{"sector": "adad"},{"sector": "adadasdsd"},{"sector": "sdsdsd"},{"origin": "sdfsf"},{"destination":"dfsfsdf"}]

let reduced = array.reduce((a, c, i) => {
  let key = Object.keys(c)[0];
  if (!(key in a)) a[key] = [];
  if (!a[key].includes(c[key])) a[key].push(c[key]);
  return a;
}, {})

console.log(reduced)
 Run code snippet
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #remove #object #key #duplicate #javascript
ADD COMMENT
Topic
Name
7+2 =