组对象数组下划线js [英] group objects array underscore js

查看:65
本文介绍了组对象数组下划线js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个看起来像这样的 URL 得到一个数组:

<预><代码>[{编号:1,产品编号:101,price_new : 80,price_old : 90},{编号:2,产品编号:101,price_new : 80,price_old : 90},{编号:3,产品编号:102,price_new : 80,price_old : 90},]

我想将其转换为:

<预><代码>[{产品编号:101,优惠:[{编号:1price_new : 80,price_old : 90},{编号:2price_new : 80,price_old : 90}]},{产品编号:102,提供:[{编号:3,price_new : 80,price_old : 90}]},]

有谁知道如何使用下划线 js 完成这项工作?很想得到一个使用下划线的解决方案,因为我们在整个项目中都使用它,而且看起来更干净,所以...

解决方案

你应该可以使用 underscore 的 groupBy 方法对它们进行分组,尽管它不会(单独)从每个记录中删除 product_id.然后,您可以获取每个键并将它们用作数组元素.

var data = [{编号:1,产品编号:101,price_new: 100,price_old: 90}, {编号:2,产品编号:101,price_new: 100,price_old: 90}, {编号:3,产品编号:102,price_new: 100,price_old: 90}, ];var grouped = _.chain(data).groupBy("product_id").map(function(offers, product_id) {//可选地从每条记录中删除 product_idvar cleanOffers = _.map(offers, function(it) {return _.omit(it, "product_id");});返回 {产品 ID:产品 ID,优惠:清洁优惠};}).价值();document.getElementById("results").textContent = JSON.stringify(grouped);

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script><pre id="results"></pre>

I'm getting an array back from an URL which looks like this:

[
   {
     id : 1,
     product_id : 101,
     price_new : 80,
     price_old : 90
   },
   {
     id : 2,
     product_id : 101,
     price_new : 80,
     price_old : 90
   },
   {
     id : 3,
     product_id : 102,
     price_new : 80,
     price_old : 90
   },
]

I would like to transform this, to:

[
   {
     product_id : 101,
     offers : [
     {
       id: 1
       price_new : 80,
       price_old : 90
     },{
       id: 2
       price_new : 80,
       price_old : 90
     }]
   },
   {
     product_id : 102,
     offers: [{
       id : 3,
       price_new : 80,
       price_old : 90
     }]
   },
]

Anyone who knows how to get this done using underscore js? Would love to get a solution using underscore cause we're using it in our entire project and it looks more clean, so...

解决方案

You should be able to use underscore's groupBy method to group them, although it won't (on its own) remove the product_id from each record. You can then take each key and use them as array elements.

var data = [{
  id: 1,
  product_id: 101,
  price_new: 100,
  price_old: 90
}, {
  id: 2,
  product_id: 101,
  price_new: 100,
  price_old: 90
}, {
  id: 3,
  product_id: 102,
  price_new: 100,
  price_old: 90
}, ];

var grouped = _.chain(data).groupBy("product_id").map(function(offers, product_id) {
  // Optionally remove product_id from each record
  var cleanOffers = _.map(offers, function(it) {
    return _.omit(it, "product_id");
  });

  return {
    product_id: product_id,
    offers: cleanOffers
  };
}).value();

document.getElementById("results").textContent = JSON.stringify(grouped);

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<pre id="results"></pre>

这篇关于组对象数组下划线js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆