【问题标题】:how to write a nested for loop using lodash?如何使用 lodash 编写嵌套的 for 循环?
【发布时间】:2018-06-12 04:37:14
【问题描述】:
arr1 = ['a', 'b', 'c'] ;
arr2 = ['1', '2', '3', '4'];

我解释

// run time
    query() {
      stuff1 = a;
      stuff2 = 1;
    }
    query() {
      stuff1 = a;
      stuff2 = 2;
    }
    query() {
      stuff1 = a;
      stuff2 = 3;
    }
    query() {
      stuff1 = a;
      stuff2 = 4;
    }
    query() {
      stuff1 = b;
      stuff2 = 1;
    }
     query() {
      stuff1 = b;
      stuff2 = 2;
    }
    query() {
      stuff1 = b;
      stuff2 = 3;
    }
    query() {
      stuff1 = b;
      stuff2 = 4;
    }
    ...
    query() {
      stuff1 = c;
      stuff2 = 6;
    }

如何编写代码?

_.map(arr1, (res) => { reutrn _.map(arr2, (res2, res1) => ... } blabla

我没有想法...

如果我使用 zipWith,a:1 b:2 c:3 ... 但我不想要它

谢谢

【问题讨论】:

    标签: javascript vue.js lodash feathersjs


    【解决方案1】:
    var arr = _.chain(arr1).map((item) => {
        return _.map(arr2, (item2) => {
           return {
              stuff1: item,
              stuff2: item2
           }
        })
    }).flatten().value();
    

    输出:

    https://jsfiddle.net/htreL1of/3/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      • 2014-11-05
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 2021-12-11
      • 2022-06-15
      相关资源
      最近更新 更多