在下划线/ lodash链中插入数组

时间:2016-12-21 20:22:08

标签: javascript underscore.js lodash

我正在尝试在lodash / underscore链中创建一个数组,但它不起作用。

示例:

import TaCoPopulator


class ViewControllerDataSource<TableOrCollectionView: PopulatorView> {
    init(with populatorView: PopulatorView) {
        self.populatorView = populatorView
        setup()
    }

    var intSelected: ((Int, IndexPath) -> Void)?
    var stringSelected: ((String, IndexPath) -> Void)?

    let dp1 = IntDataProvider {
        _ in return "Cell"
    }

    let dp2 = StringDataProvider {
        _ in return "Cell"
    }


    weak var populatorView: PopulatorView?
    var populator: Populator<MyViewPopulator>?

    func setup(){
        dp1.selected = {
            [weak self] element, indexPath in
            self?.intSelected?(element, indexPath)
        }

        dp2.selected = {
            [weak self] element, indexPath in
            self?.stringSelected?(element, indexPath)
        }

        let collectionViewCellConfig: (Any, TextCollectionViewCell, IndexPath) -> TextCollectionViewCell  = {
            element, cell, _ in
            cell.textLabel?.text = "\(element)"
            return cell
        }

        let tableViewViewCellConfig: (Any, UITableViewCell, IndexPath) -> UITableViewCell  = {
            element, cell, _ in
            cell.textLabel?.text = "\(element)"
            return cell
        }

        if  let populatorView  = populatorView as? UICollectionView {
            let  section1factory = SectionCellsFactory<Int, TextCollectionViewCell>(parentView: populatorView, provider: dp1, cellConfigurator: collectionViewCellConfig)
            let  section2factory = SectionCellsFactory<String, TextCollectionViewCell>(parentView: populatorView, provider: dp2, cellConfigurator: collectionViewCellConfig)
            self.populator = Populator(with: populatorView, sectionCellModelsFactories: [section1factory, section2factory])
        }

        if  let populatorView  = populatorView as? UITableView {
            let section1factory = SectionCellsFactory<Int, UITableViewCell>(parentView: populatorView, provider: dp1, cellConfigurator: tableViewViewCellConfig)
            let section2factory = SectionCellsFactory<String, UITableViewCell>(parentView: populatorView, provider: dp2, cellConfigurator: tableViewViewCellConfig)
            self.populator = Populator(with: populatorView, sectionCellModelsFactories: [section1factory, section2factory])
        }
    }

}

我缺少什么?

3 个答案:

答案 0 :(得分:3)

您可以使用_.times()_.fill()

&#13;
&#13;
var currentValue = "1,2,4";

var foo = _.chain(currentValue) // let's say "1,2,4"
  .split(',') // now it is ["1","2","4"]
  .max() // now it is "4"
  .times() // [undefined, undefined, undefined, undefined]
  .fill(false) // [false, false, false, false]
  .value();

console.log(foo);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>
&#13;
&#13;
&#13;

没有lodash使用ES6&#39; Array#from

&#13;
&#13;
const str = "1,2,4";

const result = Array.from({ length: Math.max(...str.split(',')) }, () => false);

console.log(result);
&#13;
&#13;
&#13;

答案 1 :(得分:3)

1)_.tap是mutate参数,最好使用_.thru

2)要获得链接结果,您必须在链的末尾调用.value()

3)所以我的建议

_.chain('1,2,4')
    .split(',')      
    .max()           
    .thru(function(maxValue) {
        return _.chain(maxValue).times(_.constant(false)).value();
    })
    .value();

如果真的不需要_.tap_.thru

_.chain('1,2,4')
    .split(',')      
    .max()           
    .times(_.constant(false))
    .value();

答案 2 :(得分:0)

这是纯粹的js解决方案。

var str = "1,2,4";

var result = Array(Math.max.apply(null, str.split(','))).fill(false);
console.log(result)