一个数组里有多个对象,如何根据对象某个相同属性,重新组成新数组,如下图和代码

问题

如下data数组,如何根据 相同的

        "shop_id"
        "shop_name"

转成data1数组

data

    var data = [{
        "cart_goods_number": 3,
        "goods_id": 3,
        "goods_name": "honor7x",
        "goods_img": "honor7x.png",
        "goods_price": 1299,
        "shop_id": 4,
        "shop_name": "honor之家"
    }, {
        "cart_goods_number": 2,
        "goods_id": 4,
        "goods_name": "honor6x",
        "goods_img": "honor6x.png",
        "goods_price": 1199,
        "shop_id": 4,
        "shop_name": "honor之家"
    }, {
        "cart_goods_number": 1,
        "goods_id": 1,
        "goods_name": "iPhonex",
        "goods_img": "iPhonex.png",
        "goods_price": 8888,
        "shop_id": 1,
        "shop_name": "iPhone之家"
    }]

data1

    var data1 = [{
        "shop_id": 4,
        "shop_name": "honor之家",
        goodsList: [{
            "cart_goods_number": 3,
            "goods_id": 3,
            "goods_name": "honor7x",
            "goods_img": "honor7x.png",
            "goods_price": 1299,
        }, {
            "cart_goods_number": 2,
            "goods_id": 4,
            "goods_name": "honor6x",
            "goods_img": "honor6x.png",
            "goods_price": 1199,
        }]
    }, {
        "shop_id": 1,
        "shop_name": "iPhone之家",
        goodsList: [{
            "cart_goods_number": 1,
            "goods_id": 1,
            "goods_name": "iPhonex",
            "goods_img": "iPhonex.png",
            "goods_price": 8888,
        }]
    }]
慕虎7371278
浏览 5713回答 7
7回答

翻翻过去那场雪

var newData = []; data.forEach(function(item) { for (var i = 0; i < newData.length; i++) { if (newData[i].shop_id === item.shop_id) { newData[i].goodsList.push({ goods_id: item.goods_id, goods_name: item.goods_name, goods_img: item.goods_img, goods_price: item.goods_price }) return; } } newData.push({ shop_id: item.shop_id, shop_name: item.shop_name, goodsList: [{ goods_id: item.goods_id, goods_name: item.goods_name, goods_img: item.goods_img, goods_price: item.goods_price }] }) }) console.log(newData)

宝慕林4294392

function func (data) { const newData = [] data.forEach(item => { const indexFound = newData.findIndex(newItem => newItem.shop_id === item.shop_id) const currentGoods = { cart_goods_number: item.cart_goods_number, goods_name: item.goods_name, goods_img: item.goods_img, goods_price: item.goods_price, goods_id: item.goods_id } if (indexFound > -1) { newData[indexFound].goodsList.push(currentGoods) } else { newData.push({ shop_id: item.shop_id, shop_name: item.shop_name, goodsList: [currentGoods] }) } }) return newData }

哈士奇WWW

function trans (data, keys) { let cache = {} return data.reduce((sum, cur) => { let obj = {} let key = keys.reduce((sum, key) => { let tmp = obj[key] = cur[key] delete cur[key] return sum + tmp }, '') let i = cache[key] if (i !== undefined) { sum[i].goodsList.push(cur) } else { cache[key] = sum.length sum.push(Object.assign(obj, {goodsList: [cur]})) } return sum }, []) } trans(data, ['shop_id', 'shop_name'])

犯罪嫌疑人X

let result = data.reduce((result, item) => { let index = result.findIndex(v => v.shop_id === item.shop_id); let {shop_id,shop_name,...single} = item; if (index === -1) { result.push({shop_id,shop_name,goods_list:[single]}); } else { result[index].goods_list.push(single); } return result; }, []); console.log(result);

互换的青春

把这个问题向前占个位置,我想知道这个问题的 Python 解决方法。

LEATH

import collections import json result = collections.defaultdict(list) for item in data: temp = dict( shop_id=item.pop('shop_id'), shop_name=item.pop('shop_name') ) result[json.dumps(temp)].append(item) result_new = list() for k, v in result.items(): temp = dict() temp.update(json.loads(k)) temp['goodsList'] = v result_new.append(temp) assert cmp(result_new, data1) == 0

哔哔one

def test(args, args1, args2, args3): data1, data3, b = list(), list(), 0 for i in args: j = i.copy() j.pop(args1) j.pop(args2) a = "{}{}".format(i.get(args1), i.get(args2)) if a not in data3: data1.append({args1: i.get(args1), args2: i.get(args2), args3: [j]}) data3.append(a) b = data3.index(a) else: data1[b][args3].append(j) return data1 print test(data, "shop_id", "shop_name", "goodsList")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python