Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove non repeating objects from array by object property name

i have array of objects and i want to remove objects which not repeating by property of name.

[
    {
        "name": "Storage Capacity",
        "value": "64GB"
    },
    {
        "name": "Manufacturer Color",
        "value": "Black / Red / White / Green / Purpule"
    },
    {
        "name": "Carrier",
        "value": "Unlocked"
    },
    {
        "name": "Carrier",
        "value": "AT&T"
    },
    {
        "name": "Brand",
        "value": "Apple"
    },
    {
        "name": "MPN",
        "value": "NA"
    },
    {
        "name": "Model",
        "value": "Iphone 11"
    },
    {
        "name": "Color",
        "value": "Black"
    },
    {
        "name": "Color",
        "value": "White"
    },
    {
        "name": "Storage Capacity",
        "value": "128GB"
    },
]

final result must be array of objects only with name : color, storage capacity, carrier

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We can use Array.reduce() to calcuate the appear count of each name,then using Array.filter() to remove the uncessary data.

// calcuate the appearance of each name
let dmap = data.reduce((a,{name}) => {
  a[name] = (a[name]??0) + 1
  return a
},{})

// filter the appear count
let result = data.filter(d => dmap[d.name] > 1)
console.log(result)
let data = [
    {
        "name": "Storage Capacity",
        "value": "64GB"
    },
    {
        "name": "Manufacturer Color",
        "value": "Black / Red / White / Green / Purpule"
    },
    {
        "name": "Carrier",
        "value": "Unlocked"
    },
    {
        "name": "Carrier",
        "value": "AT&T"
    },
    {
        "name": "Brand",
        "value": "Apple"
    },
    {
        "name": "MPN",
        "value": "NA"
    },
    {
        "name": "Model",
        "value": "Iphone 11"
    },
    {
        "name": "Color",
        "value": "Black"
    },
    {
        "name": "Color",
        "value": "White"
    },
    {
        "name": "Storage Capacity",
        "value": "128GB"
    },
]

let dmap = data.reduce((a,{name}) => {
  a[name] = (a[name]??0) + 1
  return a
},{})

let result = data.filter(d => dmap[d.name] > 1)
console.log(result)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading