vue 数据处理问题

sugar_
  • 47
let arr= [
    {
        "id": "9fdffb36-44a7-4697-9990-2923fcb34272",
        "parentId": "9fdffb36-44a7-4697-9990-2923fcb34272",
        "specItem": "32.5",
        "specName": "强度"
    },
    {
        "id": "9fdffb36-44a7-4697-9990-2923fcb34272",
        "parentId": "9fdffb36-44a7-4697-9990-2923fcb34272",
        "specItem": "32.5R",
        "specName": "强度"
    },
    {
        "id": "9fdffb36-44a7-4697-9990-2923fcb34272",
        "parentId": "9fdffb36-44a7-4697-9990-2923fcb34272",
        "specItem": "42.5",
        "specName": "强度"
    },
    {
        "id": "94755628-f416-4bd8-8423-fd0976b11a95",
        "parentId": "94755628-f416-4bd8-8423-fd0976b11a95",
        "specItem": "50KG",
        "specName": "包装"
    }
]

想要的数据格式:把相同specName的specItem拼接起来,感谢各位

let arr2 = [
    {
        "id": "9fdffb36-44a7-4697-9990-2923fcb34272",
        "parentId": "9fdffb36-44a7-4697-9990-2923fcb34272",
        "specItem": "32.5,32.5R,42.5,50KG",
        "specName": "强度"
    },
    {
        "id": "94755628-f416-4bd8-8423-fd0976b11a95",
        "parentId": "94755628-f416-4bd8-8423-fd0976b11a95",
        "specItem": "50KG",
        "specName": "包装"
    }
]
回复
阅读 1.4k
4 个回答
function handleData(arr) {
  let res = []
  let arrCopy = JSON.parse(JSON.stringify(arr)) // 避免改变原数组
  arrCopy.forEach(item => {
    if (!res.some(cItem => cItem.specName === item.specName)) {
      res.push(item)
    } else {
      res.map(cItem => {
        if (cItem.specName === item.specName) {
          cItem.specItem += `,${item.specItem}`
        }
        return cItem
      })
    }
  })
  return res
}

let res = handleData(arr)
// console.log(res, 'res');
Object.values(arr.reduce((result, item) => {
    if (result[item.specName]) {
        result[item.specName]['specItem'] += ',' + item.specItem;
    } else {
        result[item.specName] = item;
    }
    return result;
}, {}));
function merge(arr) {
    var ret = [];
    loop: for (var i = 0; i < arr.length; ++i) {
        for (var j = 0; j < ret.length; ++j) {
            if (ret[j].specName === arr[i].specName) {
                ret[j].specItem += "," + arr[i].specItem;
                continue loop; 
            }
        }
        var obj = ret[ret.length] = {};
        for (var key in arr[i]) obj[key] = arr[i][key];
    }
    return ret;
}
console.dir(merge(arr));
let arr = [
            {
                id: "9fdffb36-44a7-4697-9990-2923fcb34272",
                parentId: "9fdffb36-44a7-4697-9990-2923fcb34272",
                specItem: "32.5",
                specName: "强度",
            },
            {
                id: "9fdffb36-44a7-4697-9990-2923fcb34272",
                parentId: "9fdffb36-44a7-4697-9990-2923fcb34272",
                specItem: "32.5R",
                specName: "强度",
            },
            {
                id: "9fdffb36-44a7-4697-9990-2923fcb34272",
                parentId: "9fdffb36-44a7-4697-9990-2923fcb34272",
                specItem: "42.5",
                specName: "强度",
            },
            {
                id: "94755628-f416-4bd8-8423-fd0976b11a95",
                parentId: "94755628-f416-4bd8-8423-fd0976b11a95",
                specItem: "50KG",
                specName: "包装",
            },
        ];
        let arr1 = arr.reduce((a, b) => {
            let sameSpecName = a.find((item) => item.specName === b.specName);
            if (sameSpecName) {
                sameSpecName.specItem += "," + b.specItem;
            } else {
                a.push(b);
            }
            return a;
        }, []);
        console.log(arr1);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏