2

Edit 18.01.21 (see bolt changes)

I'm having problems figuring out a way to loop through a nested array and then to store the output in a desired order. Could you please help me to figure out a way to make this work or to point out what I'm doing wrong?

I think I'm missing a few steps in the process. For example, to break the inner loop at some point and to temporarily store my data. However, I'm not sure at what point... Important to mention, the code is intended to be used in a Google Apps Script

The toy data I'm using looks like this: I added another type of keyword to my toy data – "criss cross" and "bob ross".

var keywords =  [ [ ["claude"],["clair"],["carl"], ["criss cross"] ],
                [ ["brad"],["bob"],["bill"] ["bob ross"] ] ];

The output I'm hoping to achieve looks like this:

[ [ [ '[claude] '],[ '"claude"' ],[ '+claude' ] ],
[ [ '[clair]' ],[ '"clair"' ],[ '+clair' ] ],
[ [ '[carl]' ],[ '"carl"' ],[ '+carl' ] ],
[ [ '[criss cross]' ],[ '"criss cross"' ], [ '+criss +cross' ] ],
[ [ '[brad]' ],[ '"brad"' ],[ '+brad' ] ],
[ [ '[bob]' ],[ '"bob"' ],[ '+bob' ] ],
[ [ '[bill]' ],[ '"bill"' ],[ '+bill' ] ],
[ [ '[bob ross]' ],[ '"bob ross"' ], [ '+bob +ross' ] ] ]

However, the output I'm creating is the following:

[ [ [ '[claude]' ],[ '[clair]' ],[ '[carl]' ],[ '[brad]' ],[ '[bob]' ],[ '[bill]' ] ],
[ [ '"claude"' ],[ '"clair"' ],[ '"carl"' ],[ '"brad"' ],[ '"bob"' ],[ '"bill"' ] ],
[ [ '+claude' ],[ '+clair' ],[ '+carl' ],[ '+brad' ],[ '+bob' ],[ '+bill' ] ] ]

Here's the code I use:

var keywords =  [[["claude"],["clair"],["carl"]],
                [["brad"],["bob"],["bill"]]];

var keywords = [].concat.apply( [], keywords );
const PERMUTATION = function permuation( item ) {
    var exact = [];
    var phrase = [];
    var modyfied = [];
      
    for( let i = 0 ; i < item.length ; i++ ) {
    
      var output1 = "[" + item[i] + "]";
      exact.push([output1]);
    
      var output2 = '"' + item[i] + '"';
      phrase.push([output2]);
    
      var temp = [item[i][0].split(" ").map( i => "+" + i ).join(" ")];
      modyfied.push(temp);
    }
    return new Array( exact, phrase, modyfied ) ;
  }

  var output = PERMUTATION(keywords);
  console.log(output)

4 Answers 4

2

You could flat the arrays and map the wanted style.

const
    keywords = [[["clau de"], ["clair"], ["carl"]], [["brad"], ["bob"], ["bill"]]],
    result = keywords
        .flat(Infinity)
        .map(v => [`[${v}]`, `"${v}"`, v.split(' ').map(v => `+${v}`).join(' ')].map(v => [v]));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the input, Nina. I slightly edited my question. I added two more keywords to my toy dataset – "criss cross" and "bob ross". Your code, if I understand it correctly, will return [+bob ross] as the output. However, I'd need [+bob +ross] as the output. That's why I used .splitand .joinin my last statement. Is it possible to change your code accordingly? In fact, the keyword in question might be longer than two words. So the code needs to be flexible in this regards.
2
const keywords =  [[["claude"],["clair"],["carl"]],
                [["brad"],["bob"],["bill"]]];

keywords.flat(2).reduce((acc, key) => {
  const nestArr = [ [`[${key}]`], [`"${key}"`], [`+${key}`] ];
        
  return [...acc, nestArr];
} , []);

1 Comment

Thanks for the input, Ivan. Your code works. However, I slightly edited my question. I added two more keywords to my toy dataset – "criss cross" and "bob ross". Your code, if I understand it correctly, will return [+bob ross] as the output. However, I'd need [+bob +ross] as the output. That's why I used .splitand .joinin my last statement. Is it possible to change your code accordingly? Important to note, the keyword might be longer than just two words. ["this is a test"] might also be possible.
2

Solution

var keywords = [
  [["claude"], ["clair"], ["carl"]],
  [["brad"], ["bob"], ["bill"]],
];

let result = keywords
  .flatMap((i) => i)
  .map((keyword) =>
    ["[key]", '"key"', "+key"].map((currentKey) => [
      currentKey.replace(/key/g, keyword[0]),
    ])
  );

console.log(result);

Comments

0

I'm not sure what the outcome is supposed to be but the structure of a spreadsheet that looks like this:

COL1 COL2 COL3 COL4 COL5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40
41 42 43 44 45

Can be obtained with this code:

function getData() {
  const s=JSON.stringify(SpreadsheetApp.getActiveSheet().getDataRange().getValues());
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput('<textarea rows="12" cols="60">' + s + '</textarea>'),"Title");
}

Which ouputs something like this:

[["COL1","COL2","COL3","COL4","COL5"],
[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20],
[21,22,23,24,25],
[26,27,28,29,30],
[31,32,33,34,35],
[36,37,38,39,40],
[41,42,43,44,45]]

So you be the judge depending upon what your desired outcome is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.