nodejs json技巧

// json根据某个key的值去重
const _jsonUniq = function(arr, key1) {
  let arr1 = [arr[0]];
  arr.forEach(function (item1) {
    let flag = false;
    arr1.forEach(function (item2) {
      if (item1[key1] == item2[key1]) {
        flag = true;
        return;
      }
    })
    if (!flag ) {
      arr1.push(item1)
    }
  })
  return arr1;
}


// json根据某个key的值排序(升序)
res = allList.sort(function sortByTime(a, b) {
    return a['time']>=b['time'] ? 1:-1;
});

猜你喜欢

转载自www.cnblogs.com/stellar/p/11206478.html