数组对象按要求去除指定对象

发布于 2022-09-13 00:50:25 字数 3309 浏览 11 评论 0

原数组

[
    {
        "type": 1,
        "count": 782,
        "begin": 0,
        "end": 100,
        "countRate": 99.36,
        "proCount": 4619,
        "avgProCount": 5.91,
        "rate": 94.96,
        "amount": 21966.93
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 0,
        "count": 0,
        "countRate": 0,
        "end": 100,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 100,
        "count": 0,
        "countRate": 0,
        "end": 200,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "type": 1,
        "count": 4,
        "begin": 100,
        "end": 200,
        "countRate": 0.51,
        "proCount": 29,
        "avgProCount": 7.25,
        "rate": 2.51,
        "amount": 579.84
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 200,
        "count": 0,
        "countRate": 0,
        "end": 300,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 300,
        "count": 0,
        "countRate": 0,
        "end": 400,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 400,
        "count": 0,
        "countRate": 0,
        "end": 500,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 500,
        "count": 0,
        "countRate": 0,
        "end": 0,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "type": 1,
        "count": 1,
        "begin": 500,
        "end": 0,
        "countRate": 0.13,
        "proCount": 61,
        "avgProCount": 61,
        "rate": 2.53,
        "amount": 585.6
    }
]

要求变成这样

[
    {
        "type": 1,
        "count": 782,
        "begin": 0,
        "end": 100,
        "countRate": 99.36,
        "proCount": 4619,
        "avgProCount": 5.91,
        "rate": 94.96,
        "amount": 21966.93
    },
    {
        "type": 1,
        "count": 4,
        "begin": 100,
        "end": 200,
        "countRate": 0.51,
        "proCount": 29,
        "avgProCount": 7.25,
        "rate": 2.51,
        "amount": 579.84
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 200,
        "count": 0,
        "countRate": 0,
        "end": 300,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 300,
        "count": 0,
        "countRate": 0,
        "end": 400,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 400,
        "count": 0,
        "countRate": 0,
        "end": 500,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "type": 1,
        "count": 1,
        "begin": 500,
        "end": 0,
        "countRate": 0.13,
        "proCount": 61,
        "avgProCount": 61,
        "rate": 2.53,
        "amount": 585.6
    }
]

根据每个对象里面的begin和end对比,保留有其他值的那一个
有没有什么快捷的方式完成??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

临风闻羌笛 2022-09-20 00:50:25
let source = [
  {begin: 0, end: 100, count: 0,},
  {begin: 0, end: 100, count: 100,},
  {begin: 100, end: 100, count: 100,},
  {begin: 100, end: 100, count: 0,},
  {begin: 100, end: 200, count: 0,},
]

/**
 * 
 * @param {Object[]} arr 待处理的数组
 * @param {string[]} attrs 判断有无值的属性名称
 * @returns 
 */
function filter (arr,attrs=['count','amount']) {
  let map = new Map()//去重用
  for (let item of arr) {
    const {begin, end} = item
    const key = `${begin}:${end}`//组成唯一key
    const hasValue = attrs.some((_key) => !!item[_key])//判断有无值,此处可动态判断更多值
    if (!map.has(key)) {
      // 没有则加入
      map.set(key, item)
    } else {
      // 有则判断有无值
      hasValue && map.set(key, item)
    }
  }
  return Array.from(map.values())
}

console.log(filter(source))

结果:

守不住的情 2022-09-20 00:50:25

数据

var list = [
    {
        "type": 1,
        "count": 782,
        "begin": 0,
        "end": 100,
        "countRate": 99.36,
        "proCount": 4619,
        "avgProCount": 5.91,
        "rate": 94.96,
        "amount": 21966.93
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 0,
        "count": 0,
        "countRate": 0,
        "end": 100,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 100,
        "count": 0,
        "countRate": 0,
        "end": 200,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "type": 1,
        "count": 4,
        "begin": 100,
        "end": 200,
        "countRate": 0.51,
        "proCount": 29,
        "avgProCount": 7.25,
        "rate": 2.51,
        "amount": 579.84
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 200,
        "count": 0,
        "countRate": 0,
        "end": 300,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 300,
        "count": 0,
        "countRate": 0,
        "end": 400,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 400,
        "count": 0,
        "countRate": 0,
        "end": 500,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "amount": 0,
        "avgProCount": 0,
        "begin": 500,
        "count": 0,
        "countRate": 0,
        "end": 0,
        "proCount": 0,
        "rate": 0,
        "type": 1
    },
    {
        "type": 1,
        "count": 1,
        "begin": 500,
        "end": 0,
        "countRate": 0.13,
        "proCount": 61,
        "avgProCount": 61,
        "rate": 2.53,
        "amount": 585.6
    }
];

方法

Object.values(list.reduce((acc,cur)=>((obj = acc[cur.begin+"-"+cur.end]||=cur),(cur.amount  && cur.avgProCount && cur.count && cur.rate && cur.type && (obj=cur)),acc),{}));

结果

[
  {
    "type": 1,
    "count": 782,
    "begin": 0,
    "end": 100,
    "countRate": 99.36,
    "proCount": 4619,
    "avgProCount": 5.91,
    "rate": 94.96,
    "amount": 21966.93
  },
  {
    "amount": 0,
    "avgProCount": 0,
    "begin": 100,
    "count": 0,
    "countRate": 0,
    "end": 200,
    "proCount": 0,
    "rate": 0,
    "type": 1
  },
  {
    "amount": 0,
    "avgProCount": 0,
    "begin": 200,
    "count": 0,
    "countRate": 0,
    "end": 300,
    "proCount": 0,
    "rate": 0,
    "type": 1
  },
  {
    "amount": 0,
    "avgProCount": 0,
    "begin": 300,
    "count": 0,
    "countRate": 0,
    "end": 400,
    "proCount": 0,
    "rate": 0,
    "type": 1
  },
  {
    "amount": 0,
    "avgProCount": 0,
    "begin": 400,
    "count": 0,
    "countRate": 0,
    "end": 500,
    "proCount": 0,
    "rate": 0,
    "type": 1
  },
  {
    "amount": 0,
    "avgProCount": 0,
    "begin": 500,
    "count": 0,
    "countRate": 0,
    "end": 0,
    "proCount": 0,
    "rate": 0,
    "type": 1
  }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文