javascript - jslodash divide array basing on object property - Stack Overflow

Let's say I have an array like this one:[{ product: '...', price: 12 },{ product: '

Let's say I have an array like this one:

[
  { product: '...', price: 12 },
  { product: '...', price: 12 },
  { product: '...', price: 14 },
  { product: '...', price: 12 }
]

I'd like to group this array by price and by using lodash's groupBy I'd get below result:

[
  12: [
        { product: '...', price: 12 },
        { product: '...', price: 12 },
        { product: '...', price: 12 }
      ],
  14: [
        { product: '...', price: 14 }
      ]
]

Nice, but I'd like to have an array of three arrays because I want to save some kind of the same order like in the start array. I'd like to have this result:

[
  [
    { product: '...', price: 12 },
    { product: '...', price: 12 }
  ],
  [
    { product: '...', price: 14 }
  ],
  [
    { product: '...', price: 12 }
  ]
]

Is there a function for this?

Let's say I have an array like this one:

[
  { product: '...', price: 12 },
  { product: '...', price: 12 },
  { product: '...', price: 14 },
  { product: '...', price: 12 }
]

I'd like to group this array by price and by using lodash's groupBy I'd get below result:

[
  12: [
        { product: '...', price: 12 },
        { product: '...', price: 12 },
        { product: '...', price: 12 }
      ],
  14: [
        { product: '...', price: 14 }
      ]
]

Nice, but I'd like to have an array of three arrays because I want to save some kind of the same order like in the start array. I'd like to have this result:

[
  [
    { product: '...', price: 12 },
    { product: '...', price: 12 }
  ],
  [
    { product: '...', price: 14 }
  ],
  [
    { product: '...', price: 12 }
  ]
]

Is there a function for this?

Share Improve this question asked Apr 11, 2017 at 9:20 elzoyelzoy 5,44513 gold badges45 silver badges67 bronze badges 5
  • Whats the difference between the the first and the last array of products? – Sebastian Sebald Commented Apr 11, 2017 at 9:25
  • What have you tried so far? Can you add the code you've used to the question? – evolutionxbox Commented Apr 11, 2017 at 9:26
  • The problem is more plex. I need to have that result. The array I showed as example is much, much simpler than the array I need to work on. I tried a lot of things like grouping by, filtering etc. – elzoy Commented Apr 11, 2017 at 9:28
  • Are you only looking for lodash solution, or simple vanila js solution will also do? – gurvinder372 Commented Apr 11, 2017 at 9:38
  • I repeat the question from Sebastian Sebald: Whats the difference between the the first and the last array of products? Some other property is different or just break the result by 2 elements? – Vitalii Andrusishyn Commented Apr 11, 2017 at 10:00
Add a ment  | 

4 Answers 4

Reset to default 3

You can do something like this using lodash -

var count = 0;
console.log(
_.values(_.groupBy(_.map(myArray, (item, index) => {
        if(index == 0) return _.merge(item, { group : 0});;
        if( myArray[index-1].price == item.price )
            return _.merge(item, { group : count});
        return  _.merge(item, { group : ++count});
        }
    ), 'group'))
);

Try it here - lodash-group-consequtive

Maybe this code will helpful for you:

var myArray = [
  { product: '...', price: 12 },
  { product: '...', price: 12 },
  { product: '...', price: 14 },
  { product: '...', price: 12 }
]

var newArray = _.map(_.keyBy(myArray, "price"))

console.log(newArray)
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

With plain js you can do this using forEach() loop and save last price in variable to check if last inserted element has same price as current one.

var data = [
  { product: '...', price: 12 },
  { product: '...', price: 12 },
  { product: '...', price: 14 },
  { product: '...', price: 12 }
]

var result = [], last = null
data.forEach(function(e, i) {
  if(!this[e.price]) {
    this[e.price] = [e];
    result.push(this[e.price])
  } else {
    this[e.price].push(e)
  }
  if(last != e.price && i != 0) delete this[last]
  last = e.price
}, {})

console.log(result)

You could check the last inserted array and test the first item with the actual price. If it match, then push to the last array, otherwise push a new array to the result set.

var data = [{ product: '...', price: 12 }, { product: '...', price: 12 }, { product: '...', price: 14 }, { product: '...', price: 12 }],
    result = data.reduce(function (r, a) {
        var last = r[r.length - 1] || [{}];
        if (last[0].price === a.price) {
            last.push(a);
        } else {
            r.push([a]);
        }
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744255507a4565379.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信