Разделить массив на несколько массивов в зависимости от высоты

Однострочное решение bash, подобное , этот ответ , используя современную функцию subprocess.check_output:

def line_count(file):
    return int(subprocess.check_output('wc -l {}'.format(file), shell=True).split()[0])

0
задан Tom O. 18 January 2019 в 15:34
поделиться

1 ответ

Вы должны немного больше прояснить свою проблему, но есть возможность (адаптироваться к вашему случаю).

Я предполагаю, что items - это массив элементов jQuery (поэтому я звоню .height()) ...

РЕДАКТИРОВАТЬ: ваше редактирование показывает массив элементов HTML, вы можете преобразовать его в элементы jQuery, например, items = items.map(item => $(item))

let pageInd = 1

// loop over each page
while (items.length) {

  let // stores the items of the current page
      curPageItems = []
  ,   // current page height
      height = 0

  // loop over each elements of the current page
  curPageElmts: while (items.length) {

    // get the next element
    const nextElmt = items.shift()

    // update height
    height += nextElmt.height()

    // break loop if the height goes over 1000px
    if (height > 1000) break curPageElmts

    // otherwise add it to the current page
    curPageItems.push(nextElmt)

  }

  // append those items in the corresponding page
  // (jQuery accepts appending arrays of elements)
  $(`.page-${pageInd++}`).append(curPageItems)

}

Вы также можете избежать последней строки и сохранить результат в новом массиве. .

0
ответ дан Cinn 18 January 2019 в 15:34
поделиться
Другие вопросы по тегам:

Похожие вопросы: