하나의 키에 배열이 있고 JavaScript를 계산하는 객체 배열에서 고유 한 값을 얻는 방법은 무엇입니까?

Dewanshu

이것은 객체의 배열이 어떻게 생겼는지입니다. attributes__assigned_region여러 값의 배열이거나 단일 값일 수도 있습니다.

[{
        "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
    },

]

나는 같은 객체의 배열을 얻고 싶다.

[{
            "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 고유 한 세트 여야하며 각 세트에서 개수가 추가됩니다. attributes__assigned_region.count

소함

도움이 되었기를 바랍니다!

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)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관