Home > Software design >  How to generate line-by-line output for an arbitrary number of nested arrays in Telegram?
How to generate line-by-line output for an arbitrary number of nested arrays in Telegram?

Time:11-08

The masdata array is formed from an arbitrary number of arrays, in this case 5. Applied '% 0A' for three array elements, it works. Нow to do it for an arbitrary number of arrays?

masdata=[[["A1",10],["A2",20],["A3",30]], [["B1",50],["B2",60],["B3",70]], [["C1",100],["C2",150],["C3",200]], [["D1",25],["D2",152],["D3",457]], [["E1",18],["E2",42],["E3",55]]];

var ndata=masdata.flat();
 
answer =ndata[0] '
' ndata[1] '
' ndata[2];
 
function sendMessage(chat_id, answer) {
var url = telegramUrl   "/sendMessage?chat_id="   chat_id  "&text=" answer;
var response = UrlFetchApp.fetch(url);
 }

мой ожидаемый результат для данного массива

A1,10
A2,20
A3,30
B1,50
B2,60
B3,70
C1,100
C2,150
C3,200
D1,25
D2,152
D3,457
E1,18
E2,42
E3,55

CodePudding user response:

  let result = '';
  let arr = [[["A1", 10], ["A2", 20], ["A3", 30]], [["B1", 50], ["B2", 60], ["B3", 70]], [["C1", 100], ["C2", 150], ["C3", 200]], [["D1", 25], ["D2", 152], ["D3", 457]], [["E1", 18], ["E2", 42], ["E3", 55]]];
  arr.forEach(A => {
    A.forEach(r => {
      result  = r.join(',')   '\n';
    });
  });
  console.log(result)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

I've never played with telegrams but perhaps this will work:

function sendMessage(chat_id, answer) {
  let telegramUrl = "https://example.com";
  var url = telegramUrl   "/sendMessage?chat_id="   chat_id   "&text="   answer;
  Logger.log(url);
  var response = UrlFetchApp.fetch(url);
}

Call the below function first:

function prepareDataAndSend() {
  const arr = [[["A1", 10], ["A2", 20], ["A3", 30]], [["B1", 50], ["B2", 60], ["B3", 70]], [["C1", 100], ["C2", 150], ["C3", 200]], [["D1", 25], ["D2", 152], ["D3", 457]], [["E1", 18], ["E2", 42], ["E3", 55]]];
  let result = '';
  arr.forEach(A => {
    A.forEach(r => {
      result  = r.join(',')   '
';
    });
  });
  sendMessage(10,result);
}
  • Related