이것은 객체의 배열이 어떻게 생겼는지입니다. 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] 삭제
몇 마디 만하겠습니다