Comparing an array with another array plus adding a counter

So I’m reformatting my data and I noticed that my data isn’t quite getting restructured the way I want it to. I noticed that my results come back as

[
  {
    "name": "sites",
    "parent": null,
    "count": 3
  },
  {
    "name": "group1",
    "parent": "sites",
    "count": 3
  },
  {
    "name": "bk",
    "parent": "group1",
    "count": 3
  },
  {
    "name": "sitepages",
    "parent": "bk",
    "count": 1
  },
  {
    "name": "home.aspx",
    "parent": "sitepages",
    "count": 1
  }
]

It isn’t grabbing my “not matches”. I’ve spent so much time looking it over and I’m coming to a blank. It should be

[
  {
    "name": "sites",
    "parent": null,
    "count": 3
  },
  {
    "name": "group1",
    "parent": "sites",
    "count": 3
  },
  {
    "name": "bk",
    "parent": "group1",
    "count": 3
  },
  {
    "name": "sitepages",
    "parent": "bk",
    "count": 1
  },
  {
    "name": "home.aspx",
    "parent": "sitepages",
    "count": 1
  },
  {
    "name": "tester",
    "parent": "bk",
    "count": 1
  },
  {
    "name": "tester",
    "parent": "home.aspx",
    "count": 1
  },
  {
    "name": "_layouts",
    "parent": "bk",
    "count": 1
  },
  {
    "name": "15",
    "parent": "_layouts",
    "count": 1
  },
  {
    "name": "upload.aspx",
    "parent": "15",
    "count": 1
  },
]

I believe something is missing in my loop.

var testArr = [
  {
    Author: { Title: "Mitchell" },
    BrowserType: "FireFox",
    Created: "2017-04-25T16:39:40Z",
    pathname: "sites/group1/bk/sitepages/home.aspx"
  },
  {
    Author: { Title: "Pierre" },
    BrowserType: "Opera",
    Created: "2017-04-25T16:39:40Z",
    pathname: "sites/group1/bk/tester/home.aspx"
  },
  {
    Author: { Title: "Mizell" },
    BrowserType: "IE",
    Created: "2017-04-25T16:47:02Z",
    pathname: "sites/group1/bk/_layouts/15/upload.aspx"
  }
];

function reduceData(data) {
  var root = null;
  var newArr = null;
  var itemContainer = [];
  var masterArr = [];
  var filteredArr = [];
  data.forEach(function (props, idx) {
    //check the last character of the pathname is "/" and removes it
    if (props.pathname.charAt(props.pathname.length - 1) === "/") {
      props.pathname = props.pathname.substring(0, props.pathname.length - 1);
    }

    //lowercase the pathname + split into strings
    props.pathname = props.pathname.toLowerCase().split("/");

    //format the pathname
    var lastItem = "";
    newArr = props.pathname.reduce(function (acc, props, index) {
      if (acc.length === 0) {
        acc.push({ name: props, parent: null, count: 1 });
        lastItem = props;
      } else {
        acc.push({ name: props, parent: lastItem, count: 1 });
        lastItem = props;
      }
      return acc;
    }, []);

    //The first iteration 
    if (idx === 0) {
      itemContainer = newArr;
    } else {
      for (var i = 0; i < itemContainer.length; i++) {
        // Loop for newArr
        for (var j = 0; j < newArr.length; j++) {
          //compare the element of each and every element from both of the arrays
          //console.log(masterArr[i], newArr[j]);
          if (
            itemContainer[i].name === newArr[j].name &&
            itemContainer[i].parent === newArr[j].parent
          ) {
            //Match
            masterArr[i] = {
              name: itemContainer[i].name,
              parent: itemContainer[i].parent,
              count: itemContainer[i].count++
            };
          } else {
            //Doesn't Match
            masterArr[i] = {
              name: itemContainer[i].name,
              parent: itemContainer[i].parent,
              count: itemContainer[i].count
            };
          }
        }
      }
    }
 });
  console.log(masterArr)
}
reduceData(testArr)

Answer

ok.. I revamp your code a little..

delete the if else after the //The first iteration, and use this instead..

newArr.forEach((newEl) => {
      const matchIdx = masterArr.findIndex((masterEl) => masterEl.name === newEl.name && masterEl.parent === newEl.parent);
      if(matchIdx < 0){
        masterArr.push(newEl);
      }
      else {
        masterArr[matchIdx].count = masterArr[matchIdx].count + 1;
      }
    });

Leave a Reply

Your email address will not be published. Required fields are marked *