将一个数组与另一个数组进行比较并添加一个计数器

时间:2021-03-10 22:14:00

标签: javascript loops for-loop ecmascript-6

所以我正在重新格式化我的数据,但我注意到我的数据并没有按照我想要的方式进行重组。我注意到我的结果返回为

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

我相信我的循环中缺少某些东西。

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)

1 个答案:

答案 0 :(得分:0)

好的..我稍微修改了你的代码..

删除//第一次迭代后的if else,用这个代替..

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;
      }
    });