Skip to content
Advertisement

Create a loop to compile multiple records into a new variable

At the moment I am fetching metadata attached to individual record numbers. The process I am using works, but its somewhat cumbersome, especially for larger batches of records:

var a = 'metadatafrom_123456_record-as_record:record_name,record_date';
var b = 'metadatafrom_23456_record-as_record:record_name,record_date';
var c = 'metadatafrom_789456_record-as_record:record_name,record_date';
var allData = [...a, ...b, ...c];
console.log(allData);

I am trying to improve this process by creating a loop or sorts to insert each record number to get the same result as above in allData.

var numbers = [123456, 23456, 789456];
var allData = [];

for (i = 0; i < numbers.length; i++) {
  console.log(numbers[i]);
  allData = metadatafrom_numbers[i]_record-as_record:record_name,record_date;
} 

console.log(allData);

However, I am not getting allData in console or any error.

Advertisement

Answer

allData.push() is good

var numbers = [123456, 23456, 789456];
var allData = [];

for (let num of numbers) {
  allData.push(`metadatafrom_${num}_record-as_record:record_name,record_date`);
}

console.log(allData);
Advertisement