问题
如下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,
}]
}]
7 回答
翻翻过去那场雪
TA贡献2065条经验 获得超14个赞
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
TA贡献2021条经验 获得超8个赞
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
TA贡献1799条经验 获得超6个赞
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
TA贡献2080条经验 获得超4个赞
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);
LEATH
TA贡献1936条经验 获得超6个赞
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
TA贡献1854条经验 获得超8个赞
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")
添加回答
举报
0/150
提交
取消