0

i am trying to count object has value added === true by child_name witch javascript function useful please guide me ?

This is main Array this array has object under object has items and under items has another object key and value how to count object only if added === true by child_name

[
  {
    "id": 38,
    "items": {
      "Big Box": [
        [
          {
            "id": 1153,
            "parent_name": "Big Box",
            "child_name": "",
            "added": true
          }
        ],
        [
          {
            "id": 1153,
            "parent_name": "Big Box",
            "child_name": "",
            "added": false
          }
        ],
        [
          {
            "id": 1153,
            "parent_name": "Big Box",
            "child_name": "",
            "added": true
          }
        ]
      ]
    }
  },
  {
    "id": 37,
    "items": {
      "Drawers": [
        [
          {
            "id": 1108,
            "parent_name": "Drawers",
            "child_name": "Small "
          },
          {
            "id": 1109,
            "parent_name": "Drawers",
            "child_name": "Medium",
            "added": true
          },
          {
            "id": 1110,
            "parent_name": "Drawers",
            "child_name": "Large"
          }
        ]
      ],
      "Mirror": [
        [
          {
            "id": 1141,
            "parent_name": "Mirror",
            "child_name": "Large",
            "added": false
          },
          {
            "id": 1142,
            "parent_name": "Mirror",
            "child_name": "Small",
            "added": true
          },
          {
            "id": 1143,
            "parent_name": "Mirror",
            "child_name": "Medium",
            "added": false
          }
        ],
        [
          {
            "id": 1141,
            "parent_name": "Mirror",
            "child_name": "Large"
          },
          {
            "id": 1142,
            "parent_name": "Mirror",
            "child_name": "Small"
          },
          {
            "id": 1143,
            "parent_name": "Mirror",
            "child_name": "Medium",
            "added": true
          }
        ]
      ],
      "Stand": [
        [
          {
            "id": 1145,
            "parent_name": "Stand",
            "child_name": "",
            "added": true
          }
        ],
        [
          {
            "id": 1145,
            "parent_name": "Stand",
            "child_name": "",
            "added": true
          }
        ],
        [
          {
            "id": 1145,
            "parent_name": "Stand",
            "child_name": "",
            "added": true
          }
        ]
      ]
    }
  }
]

output should like this count object by child_name and added === true

    [
      {
       "parent_name":"Big Box",
       "child_name":"",
       "count":2
      },
      {
       "parent_name":"Drawers",
       "child_name":"Medium",
       "count":1
      },
      {
       "parent_name":"Mirror",
       "child_name":"Small",
       "count":1
      },
      {
       "parent_name":"Mirror",
       "child_name":"Medium",
       "count":1
      },
      {
       "parent_name":"Stand",
       "child_name":"",
       "count":3
      }
    ]
1
  • What have you tried to solve this problem? Please see stackoverflow.com/help/how-to-ask to know how to ask good question. Commented Jun 14, 2020 at 9:33

3 Answers 3

1

You can do this with reduce. Here is my try:

var data=[ { "id": 38, "items": { "Big Box": [ [ { "id": 1153, "parent_name": "Big Box", "child_name": "", "added": true } ], [ { "id": 1153, "parent_name": "Big Box", "child_name": "", "added": false } ], [ { "id": 1153, "parent_name": "Big Box", "child_name": "", "added": true } ] ] } }, { "id": 37, "items": { "Drawers": [ [ { "id": 1108, "parent_name": "Drawers", "child_name": "Small " }, { "id": 1109, "parent_name": "Drawers", "child_name": "Medium", "added": true }, { "id": 1110, "parent_name": "Drawers", "child_name": "Large" } ] ], "Mirror": [ [ { "id": 1141, "parent_name": "Mirror", "child_name": "Large", "added": false }, { "id": 1142, "parent_name": "Mirror", "child_name": "Small", "added": true }, { "id": 1143, "parent_name": "Mirror", "child_name": "Medium", "added": false } ], [ { "id": 1141, "parent_name": "Mirror", "child_name": "Large" }, { "id": 1142, "parent_name": "Mirror", "child_name": "Small" }, { "id": 1143, "parent_name": "Mirror", "child_name": "Medium", "added": true } ] ], "Stand": [ [ { "id": 1145, "parent_name": "Stand", "child_name": "", "added": true } ], [ { "id": 1145, "parent_name": "Stand", "child_name": "", "added": true } ], [ { "id": 1145, "parent_name": "Stand", "child_name": "", "added": true } ] ] } }]

var result = Object.values(data.reduce((acc, {items})=>{
  Object.values(items).forEach(values=>{		
    values.flat().filter(g=>g.added).forEach((t,i,self)=>{
       key = `${t.parent_name}|${t.child_name}`
       acc[key]= {};
       acc[key]['parent_name'] = t.parent_name;
       acc[key]['child_name'] = t.child_name;
       acc[key]['count'] = self.filter(c=>c.added && c.child_name==t.child_name).length;
     });
  });
 return acc;
},{}));

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0

just iterate over all elements with reduce function and count pairs child_name and parent_name

const res = arr.reduce((agg, v) => { 
  for(const key in v.items) {
    if (!Array.isArray(v.items[key])) continue
    v.items[key].forEach(v => v.forEach(v => {
      if (typeof v !== 'object') return
      const value = {
        parent_name: v.parent_name,
        child_name: v.child_name,
        count: 1
      }
      const key = JSON.stringify(value)
      if (!agg[key]) 
        agg[key] = value 
      else
        agg[key].count++
    }))
  }
  return agg
}, {})

console.log(Object.values(res))

Comments

0

First, note that the items such as "Big Box" are array inside an array. You might like to simply that make it an array of objects and do this:

var data = [
  {
    "id": 38,
    "items": {
      "Big Box": [
          {
            "id": 1153,
            "parent_name": "Big Box",
            "child_name": "",
            "added": true
          },
          {
            "id": 1153,
            "parent_name": "Big Box",
            "child_name": "",
            "added": false
          },
          {
            "id": 1153,
            "parent_name": "Big Box",
            "child_name": "",
            "added": true
          }
      ]
    }
 
  }
]
var output = [];
for(let i =0; i< data.length; i++)
for(var key in data[i].items){
  var x = data[0].items[key].filter(i=>i.added === true);
  var count = x.length;
  output.push({...x[0], ...{count}})
}
console.log(output)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.