Javascript array alllocation [duplicate] - javascript

This question already has answers here:
Are Javascript arrays sparse?
(7 answers)
Closed 6 years ago.
I was dealing with a problem and got to the point where I thought I could use the Ids to my entity instances as array indices for easy lookup.
var myArray = [];
myArray[obj.Id] = true;
Assume obj.Id is 1000 here, so will be myArray.length. Am I allocating 1000 bytes for a single boolean value here or is it just returning the maximum index as length?

It won't be allocating so many bytes.
But what you are really looking for is an key-value object like
var myObj = {};
myObj[obj.Id] = true;
//then to access
console.log(myObj[obj.Id])

Related

how to get length of WeakMap? [duplicate]

This question already has answers here:
How to iterate over a WeakMap?
(3 answers)
Closed 9 months ago.
I have a WeakMap like this:
let obj = new WeakMap();
let objKey1={"a":1};
let objKey2={"b":2};
let objKey3={"c":3};
obj.set(objKey1,"value1");
obj.set(objKey2,"value2");
obj.set(objKey3,"value3");
Is there a way to get the length / number of keys stored in obj?
I tried Object.keys(obj).length but it returns 0
From the documentation
But because a WeakMap doesn't allow observing the liveness of its keys, its keys are not enumerable. There is no method to obtain a list of the keys.
It should therefore follow that there is no way to get the number of keys either.

How do I fill a new array with n distinct empty arrays, in a concise one-line initialization statement? [duplicate]

This question already has answers here:
How do you easily create empty matrices javascript?
(17 answers)
fill an array with arrays programmatically
(5 answers)
Array.prototype.fill() with object passes reference and not new instance
(7 answers)
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 1 year ago.
Here is where I am stuck. I want to take this statement and revise it in a manner that the empty array I fill (which I surmise might not work with dynamic values), will initialize bucket to the n distinct empty arrays.
How do I do this? Is there a way to make this fill method behave in the intended manner?
let radix = 10;
let badBucket = [...Array(radix).fill([])];
let goodBucket = JSON.parse(JSON.stringify([...Array(radix).fill([])]));
badBucket[3].push(33);
goodBucket[3].push(33);
console.log(JSON.stringify(badBucket));
console.log(JSON.stringify(goodBucket));
You can use the callback function of Array.from.
let length = 5;
let res = Array.from({length}, _=>[]);
console.log(res);
Try:
let radix = 10;
let bucket = [...Array(radix)].map(e=>[])
bucket[0].push(1)
console.log(JSON.stringify(bucket));

Struggling with Javascript Object Array adding keys [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 5 years ago.
I have this snippet of code:
var arrLiteData = [];
for(ii=0;ii<10;ii++)
{
arrLiteData.push({ii:{"field1":100,"field2":ii}});
}
...but instead of ii taking the increasing numeric value of ii, the array just holds the actual variable name, like this:
[{"ii":{"field1":100,"field2":0}},{"ii":{"field1":100,"field2":1}}...etc, etc...
What am I doing wrong?
Many thanks.
Quotes are optional for javascript object keys, so
{ii:{"field1":100,"field2":ii}} is the same as
{"ii":{"field1":100,"field2":ii}} or even
{ii:{field1:100,field2:ii}}. They are just need if you have non alphanumeric characters.
To solve this you could either use a computed key if you're transpiling your code or targeting recent navigators:
{[ii]:{"field1":100,"field2":ii}}
Or build the object in two steps:
var arrLiteData = [];
for(ii=0;ii<10;ii++)
{
var obj = {};
obj[ii] = {"field1":100,"field2":ii};
arrLiteData.push(obj);
}

Can you create an associative array in JavaScript? [duplicate]

This question already has an answer here:
JSON Stringify Removing Data From Object
(1 answer)
Closed 6 years ago.
I am trying to set up an associative array in JavaScript and then store it in HTML5 Local Storage:
var student = [];
student["david"] = "He is doing very well";
localStorage['student'] = JSON.stringify(student);
var data = JSON.parse(localStorage['student']);
alert(data.david);
There is no such thing as an "associative array" in JavaScript. You are using a regular array which requires the use of numeric indexes, and adding arbitrary properties to it, which are ignored when you serialize the array to JSON.
The JSON-stringified result will be: "[]".
You need to use a real object:
var student = {}

copying an array of objects in javascript [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Copying array by value in javascript
How to copy an array of objects to another array in Javascript?
var qwerty1 = arr;
var qwerty2 = arr;
Both qwerty1 and qwerty2 may look different but point to the same reference.
I read somewhere that "assigning a boolean or string to a variable makes a copy of that value, while assigning an array or an object to a variable makes a reference to the value." So my two arrays post different operations return the same objects.
Any light in this regard?
The idiomatic way to copy an array in Javascript is to use concat:
var qwerty1 = arr.concat();
var qwerty2 = arr.concat();

Categories