خرید بک لینک

Vote count: 0

I am using lodash, I wanted to group an array of objects according to the sum of it's property width.

I have this data set

[
    {id: 0, width: 2},
    {id: 1, width: 2},
    {id: 2, width: 4},
    {id: 3, width: 1},
    {id: 4, width: 3}
]

Actually this is to build rows and columns, and the maximum width of a row is 4

The expected output would be:

[
    [{id: 0, width: 2}, {id: 1, width: 2}],
    [{id: 2, width: 4}],
    [{id: 3, width: 1}, {id: 4, width: 3}]
]

How do I go about this?

Was thinking like:

const maxWidth = 4
let currentRowWidth = 0

let newData = _.map(source, data => {
  let array = []
  if (currentRowWidth === 0) {
    currentRowWidth += data.width
    ...
    ...
  }
})

asked 31 secs ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 13 تير 1395 ساعت: 13:16

صفحه بندی