How to get distinct values from an array of objects which has an Array inside in on one key and count JavaScript?

Viewed 51

This how what array of object looks like. attributes__assigned_region is a array of multiple values or it can be a single value too

[{
        "attributes__assigned_region": [
            "w",
             ],
        "count": 1
    },
    {
        "attributes__assigned_region": [
            "x"
        ],
        "count": 1
    },
    {
        "attributes__assigned_region": [
            "y"
        ],
        "count": 1
    },
    {
        "attributes__assigned_region": [
            "z"
        ],
        "count": 2
    },
    {
        "attributes__assigned_region": [
            "x",
            "z",
        ],
        "count": 2
    },
      {
        "attributes__assigned_region": [
            "y",
            "z",
        ],
        "count": 2
    },
      {
        "attributes__assigned_region": [
            "x",
            "y",
        ],
        "count": 4
    },
     {
        "attributes__assigned_region": [
            "w",
            "y",
        ],
        "count": 3
    },
     {
        "attributes__assigned_region": [
            "x",
            "y",
            "z"
        ],
        "count": 5
    },
      {
        "attributes__assigned_region": [
            "x",
            "y",
            "w"
        ],
        "count": 5
    },

]

I want to get array of object like

[{
            "attributes__assigned_region": [
                "w",
               ],
            "count": 9 // the total from all count where w is avalaible (1 + 3 + 5 )
        },
        {
            "attributes__assigned_region": [
                "x"
            ],
            "count": 17 // the total from all count where y is available (1 + 2 + 4 + 5 + 5)
        },
        {
            "attributes__assigned_region": [
                "y"
            ],
            "count": 20 // the total from all count where y is available (1 + 2 + 4 + 3 + 5 + 5)
        },
        {
            "attributes__assigned_region": [
                "z"
            ],
            "count": 11 // the total from all count where y is available (2 + 2 + 2 + 5)
        }]

attributes__assigned_region needs to be a unique set and count will get added from each attributes__assigned_region.count

1 Answers

Hope this helps!

let object = [{
    "attributes__assigned_region": [
      "w",
    ],
    "count": 1
  },
  {
    "attributes__assigned_region": [
      "x"
    ],
    "count": 1
  },
  {
    "attributes__assigned_region": [
      "y"
    ],
    "count": 1
  },
  {
    "attributes__assigned_region": [
      "z"
    ],
    "count": 2
  },
  {
    "attributes__assigned_region": [
      "x",
      "z",
    ],
    "count": 2
  },
  {
    "attributes__assigned_region": [
      "y",
      "z",
    ],
    "count": 2
  },
  {
    "attributes__assigned_region": [
      "x",
      "y",
    ],
    "count": 4
  },
  {
    "attributes__assigned_region": [
      "w",
      "y",
    ],
    "count": 3
  },
  {
    "attributes__assigned_region": [
      "x",
      "y",
      "z"
    ],
    "count": 5
  },
  {
    "attributes__assigned_region": [
      "x",
      "y",
      "w"
    ],
    "count": 5
  },
]

let newObject = [];
let trackedItem = [];

object.forEach(elem => {
  elem.attributes__assigned_region.forEach(ele => {
    if (trackedItem.indexOf(ele) === -1) {
      let temObject = {
        "attributes__assigned_region": [
          ele
        ],
        "count": elem.count
      }
      trackedItem.push(ele)
      newObject.push(temObject)
    } else {
      newObject.forEach(object => {
        object.attributes__assigned_region.forEach(el => {
          if (ele === el) {
            object.count = object.count + elem.count
          }
        })
      })
    }

  })
})

console.log(newObject)

Related