Q:

Iterating on nested arrays in objects to collate informaton

I have the following array of objects:
let array = [
{key: 'Newcastle', values[
  {key: 'ID1000', values[
    {name: 'Jeff', cust_id: "ID1000", status: 'sold'},
    {name: 'Jeff', cust_id: "ID1000", status: 'pending'},
  ]}
  {key: 'ID2000', values [
    {name: 'Bob', cust_id: "ID2000", status: 'sold'}
  ]}
]}
{key: 'London', values [
 {key: 'ID3000', values[
  {name: 'Gary', cust_id: "ID3000", status: 'sold'},
 ]}
 {key: 'ID4000', values[
  {name: 'Mary', cust_id: "ID4000", status: 'interest'},
  {name: 'Mary', cust_id: "ID4000", status: 'interest'},
  {name: 'Mary', cust_id: "ID4000", status: 'pending'},
 ]}
]}
]

I have been trying to refactor it into something like:
[
 {Location: 'Newcastle, customers: 2, sold: 2, pending: 1, interest: 0},
 {Location: 'London', customers: 2, sold: 1, pending: 1, interest: 2}
]

So I am attempting to count the number of status events and collate them accordingly.
I get lost when I try to iterate on the nested arrays and then when trying to bubble up the results of the iteration to a final object. The closest I have got is:
function transform(array) {
      let arr = []
      array.forEach(function(x) {
        function soldCount() {
          x.values.forEach(function(x) {

            let sold = x.values.forEach(function(x) {
              let soldTrue = 0
              if (x.status === "sold") {
                soldTrue++
              }
              console.log(soldTrue)
              if (soldTrue > 0) {
                return soldTrue
              }
            })

          })
        }
        let obj = {
          location: x.key,
          customers: x.values.length,
          sold: soldCount()
        }

        arr.push(obj)
      })
      return arr
    }

This tries to iterate on each array in the objects and attempts to return a number for how many of the 'sold' status it finds. The console statement does return a number but it returns multiple entries for each item in the array due to 'forEach'.
I am swamped in a number of forEach loops iterating on nested arrays. I suspect that this might not be the correct methodology for what I am trying to achieve.

A:

You could take an object counting status and build a new objects.

var array = [{ key: 'Newcastle', values: [{ key: 'ID1000', values: [{ name: 'Jeff', cust_id: "ID1000", status: 'sold' }, { name: 'Jeff', cust_id: "ID1000", status: 'pending' },] }, { key: 'ID2000', values: [{ name: 'Bob', cust_id: "ID2000", status: 'sold' }] }] }, { key: 'London', values: [{ key: 'ID3000', values: [{ name: 'Gary', cust_id: "ID3000", status: 'sold' },] }, { key: 'ID4000', values: [{ name: 'Mary', cust_id: "ID4000", status: 'interest' }, { name: 'Mary', cust_id: "ID4000", status: 'interest' }, { name: 'Mary', cust_id: "ID4000", status: 'pending' }] }] }],
    result = array.map(({ key: Location, values }) => {
        var data = { Location, customers: 0, sold: 0, pending: 0, interest: 0 };
        values.forEach(({ values }) => {
            data.customers++;
            values.forEach(({ status }) => data[status]++);
        });
        return data;
    });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

