Search code examples
javascriptvue.jslodashfeathersjs

how to write a nested for loop using lodash?


arr1 = ['a', 'b', 'c'] ;
arr2 = ['1', '2', '3', '4'];

and I expact

// 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;
    }

how to write code?

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

I have no ideas...

if i use zipWith, a:1 b:2 c:3 ... but i don't want it

Thanks


Solution

  • var arr = _.chain(arr1).map((item) => {
        return _.map(arr2, (item2) => {
           return {
              stuff1: item,
              stuff2: item2
           }
        })
    }).flatten().value();
    

    Output:

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