Dec 08, 2021 07:29 AM
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.
Solved! Go to Solution.
Dec 09, 2021 09:16 PM
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:
Dec 09, 2021 12:57 AM
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
Dec 09, 2021 03:29 AM
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));
Dec 09, 2021 12:41 PM
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.
Dec 09, 2021 01:27 PM
You can also look into array.slice() as a quick way of making a copy of an array.
Dec 09, 2021 09:16 PM
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:
Dec 09, 2021 11:39 PM
Yeah, sometimes spread operator is very useful to avoid redundant pieces of code. One-line button selector
Dec 11, 2021 05:47 AM
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