怎么判断JSON对象中value存在重复值次数多少

怎么判断JSON对象中value存在重复值次数多少?

假设是等待处理,判断这个相同值总共多少个,然后新建 JSON,如下:


原数据:


[

    {name: "fcf294131", key: "处理进度", value: "等待处理"},

    {name: "fcf294131", key: "处理进度", value: "正在处理"},

    {name: "fcf294131", key: "处理进度", value: "等待处理"},

    {name: "fcf294131", key: "处理进度", value: "完成"},

    {name: "fcf294131", key: "处理进度", value: "等待处理"}

]

处理


[

    {key:"等待处理",len:3},

    {key:"正在处理",len:1},

    {key:"完成",len:1}

]

这个方法怎么写?


小怪兽爱吃肉
浏览 1422回答 4
4回答

幕布斯6054654

如果已知需要处理的值的话var arrStr=JSON.stringify([    {name: "fcf294131", key: "处理进度", value: "等待处理"},    {name: "fcf294131", key: "处理进度", value: "正在处理"},    {name: "fcf294131", key: "处理进度", value: "等待处理"},    {name: "fcf294131", key: "处理进度", value: "完成"},    {name: "fcf294131", key: "处理进度", value: "等待处理"}])res=[    {key: "等待处理", len: arrStr.split("等待处理").length-1},    {key: "正在处理", len: arrStr.split("正在处理").length-1},    {key: "完成", len: arrStr.split("完成").length-1}]当然没有考虑性能,只是取巧简洁一点hhhh

温温酱

let arr = [], // 初始数据集    res = {}; // 结果arr.forEach(item => {    if(!res[item.value]) {        res[item.value] = {            key: item.value,            len: 1        }    } else {        res[item.value].len++;    }})res = Object.values(res);

大话西游666

写了一个简单易懂的:function countValue (arr) {&nbsp; var does = {&nbsp; &nbsp; key: '等待处理',&nbsp; &nbsp; len: 0&nbsp; };&nbsp; &nbsp;var doing = {&nbsp; &nbsp; key: '正在处理',&nbsp; &nbsp; len: 0&nbsp; &nbsp;};&nbsp; &nbsp;var done = {&nbsp; &nbsp; key: '已处理',&nbsp; &nbsp; len: 0&nbsp; &nbsp;};&nbsp; var newArr = [];&nbsp; for(var i =0;i <arr.length; i++) {&nbsp; &nbsp; switch(arr[i].value) {&nbsp; &nbsp; &nbsp; case '等待处理': does.len++;break;&nbsp; &nbsp; &nbsp; case '正在处理': doing.len++;break;&nbsp; &nbsp; &nbsp; case '已处理': done.len++;break;&nbsp; &nbsp; }&nbsp; }&nbsp; newArr.push(does);&nbsp; newArr.push(doing);&nbsp; newArr.push(done);&nbsp; return newArr}结果如图:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript