JS将JSON中相同的值提取出来,重新组合到一个新数组

function goodsidsRecombineToNewJson(old_json) {
	var goodsids = {};
	for (var code in old_json) {
		var goodsid = old_json[code].gid;
		if (!goodsids[goodsid]) {
			goodsids[goodsid] = [];
		}
		goodsids[goodsid].push(old_json[code]);
	}
	return goodsids;
}
// Example:
var old_json = {
	"0000008422087777":{"gid":1,"bid":38,"code":"0000008422087777","sta":0,"pt":0,"tm":1681822258,"bind":"1个中码,2个小码"},"0000008463360354":{"gid":1,"bid":38,"code":"0000008463360354","sta":0,"pt":0,"tm":1681822291,"bind":"1个中码,2个小码"},"1000008751140187":{"gid":1,"bid":41,"code":"1000008751140187","sta":0,"pt":1,"tm":1681822310,"bind":"1个中码,2个小码"},"0000008490947091":{"gid":1,"bid":38,"code":"0000008490947091","sta":0,"pt":0,"tm":1681822180,"bind":"1个中码,2个小码"},"0000008493498239":{"gid":3,"bid":38,"code":"0000008493498239","sta":0,"pt":0,"tm":1681822234,"bind":"1个中码,2个小码"},"1000008722001324":{"gid":3,"bid":41,"code":"1000008722001324","sta":0,"pt":1,"tm":1681822251,"bind":"1个中码,2个小码"},"1000008779093484":{"gid":4,"bid":41,"code":"1000008779093484","sta":0,"pt":1,"tm":1681822174,"bind":"1个中码,2个小码"},"0000008455002115":{"gid":4,"bid":38,"code":"0000008455002115","sta":0,"pt":0,"tm":1681822177,"bind":"1个中码,2个小码"},"1000008770652147":{"gid":5,"bid":41,"code":"1000008770652147","sta":0,"pt":1,"tm":1681822139,"bind":"1个中码,2个小码"},"0000008473019575":{"gid":5,"bid":38,"code":"0000008473019575","sta":0,"pt":0,"tm":1681822143,"bind":"1个中码,2个小码"},"0000008462375945":{"gid":6,"bid":38,"code":"0000008462375945","sta":0,"pt":0,"tm":1681822144,"bind":"1个中码,2个小码"}
}
// output result:
{
	"1":[
		{"gid":1,"bid":38,"code":"0000008422087777","sta":0,"pt":0,"tm":1681822258,"bind":"1个中码,2个小码"},
		{"gid":1,"bid":38,"code":"0000008463360354","sta":0,"pt":0,"tm":1681822291,"bind":"1个中码,2个小码"},
		{"gid":1,"bid":41,"code":"1000008751140187","sta":0,"pt":1,"tm":1681822310,"bind":"1个中码,2个小码"},
		{"gid":1,"bid":38,"code":"0000008490947091","sta":0,"pt":0,"tm":1681822180,"bind":"1个中码,2个小码"}
	],
	"3":[
		{"gid":3,"bid":38,"code":"0000008493498239","sta":0,"pt":0,"tm":1681822234,"bind":"1个中码,2个小码"},
		{"gid":3,"bid":41,"code":"1000008722001324","sta":0,"pt":1,"tm":1681822251,"bind":"1个中码,2个小码"}
	],
	"4":[
		{"gid":4,"bid":41,"code":"1000008779093484","sta":0,"pt":1,"tm":1681822174,"bind":"1个中码,2个小码"},
		{"gid":4,"bid":38,"code":"0000008455002115","sta":0,"pt":0,"tm":1681822177,"bind":"1个中码,2个小码"}
	],
	"5":[
		{"gid":5,"bid":41,"code":"1000008770652147","sta":0,"pt":1,"tm":1681822139,"bind":"1个中码,2个小码"},
		{"gid":5,"bid":38,"code":"0000008473019575","sta":0,"pt":0,"tm":1681822143,"bind":"1个中码,2个小码"}
	],
	"6":[{"gid":6,"bid":38,"code":"0000008462375945","sta":0,"pt":0,"tm":1681822144,"bind":"1个中码,2个小码"}]
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值