How to put an Array into Object?

Hello everyone!

This is probably a simple question, but I could not figure out the answer. I have an Object and two arrays:

const arr = [1,2,3];
const arr2 = [];
const obj = {};

obj.id = arr[0];
obj.fields = {};
obj.fields["Array items"] = arr; 
arr2.push(obj);

console.log(arr2); //array contains 3 items as expected
arr.length = 0; //empty array
console.log(arr2); //array is empty

As I understand the Object instance keeps only a pointer to the array. Is it possible with an Object to keep a copy of array contents and how to achive that?

Thank you in advance.

Found my mistake. Instead of

arr.length = 0; //empty array

should use

arr = []; //empty array

In this case a new reference is created and values in arr2 stay the same.
Learned from this article: Learning how references work in JavaScript | by Naveen Karippai | Medium

1 Like

Hi,

Surprised if it working, because you declared arr as const.

I quit learing programming many years ago, after Turbo Pascal and Visual Basic, because i couldn’t understand OOP. When I started working with Airtable, i had to learn all those “object of arrays of objects” and struggled a lot, especially, when I had some output data and need to write it to the table - where i should put “id”, “fields”, their name, and “[”, “{” , in what order.
So maybe my advice will be useful and save you some time.

Working with data arrays here needs other type of thinking. Usually you don’t need to empty arrays or reassign, or “destruct” them using index number.

typically, you have some imput array, need to process it, and turn the result into correct form of output array.

i recommend to learn and understand “array-helpers” like map(), filter(), includes() (in 1st turn). Also find(), flatMap(), sort(), some(), reduce(). then you will have no troubles with the rest. And “arrow-functions”, of course

For example, i’m using that “template” from start. Even if your code is quite complex, using function to form “updates” array saves your time in the final part of script.

let table = base.getTable(‘ANY_TABLE’);
let query = await table.selectRecordsAsync({fields:[‘SOURCE’]});
function setValue(rec){return {‘id’:rec.id,‘fields’:{‘TARGET’:rec.getCellValue(‘SOURCE’)}}};
let updates=query.records.map(setValue);
while (updates.length) await table.updateRecordsAsync(updates.splice(0, 50));

1 Like

Hi @Alexey_Gusev! Thanks for your comment. Sure, I forget to mention that change of array declaration from const to let. Also, thanks a lot for your tips. It is worth studying.

1 Like

You can also look into array.slice() as a quick way of making a copy of an array.

2 Likes

Another option is the spread operator ( ... ):

let firstArray = [1, 2, 3];
let secondArray = [...firstArray];

I used this recently to make a quick copy of the records from a table query so that I could sort them (which can’t be done on the original records array because it’s read-only).

While searching for other options just now, I found this article that breaks down the pros and cons of a variety of methods:

3 Likes

Yeah, sometimes spread operator is very useful to avoid redundant pieces of code. One-line button selector

1 Like

That piece of code is quite useless, takes text from record by ID and create multiple new records with it’s each word, not including letter “e”.
But it’s a good demo of basic methods and functions useful here

1 Like

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.