how to get length of WeakMap? [duplicate] - javascript

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.

Related

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));

convert array(2) that has sorted object to object [duplicate]

This question already has answers here:
Does JavaScript guarantee object property order?
(13 answers)
Does ES6 introduce a well-defined order of enumeration for object properties?
(3 answers)
Closed 1 year ago.
I'm trying to sort an object from db
MyOjbect : {
key: {
val1:"value",
val2:"",
val3:time,
val4:...
...}
}
and I sorted the object by val3
const sorted = Object.entries(MyObject)
.sort((x,y) => {x[1].val3 - y[1].val3} );
and got an array type.
0:(2) ["key" , {val1:...val2:...}]
1:(2) ["key" , {val1:...val2:...}]
...
I tried reduce() to make it back to the object type, but it changes to be in unsorted order
How can I keep the order sorted and change the type to the object?
You can't. An object doesn't keep the sort order as an Array does it. If you want to have the keys sorted you need to use an Array.

Can't force object keys to be integer [duplicate]

This question already has answers here:
Keys in Javascript objects can only be strings?
(8 answers)
Closed 4 years ago.
const obj = {
15: 100
};
for(let key in obj)
console.log(key, typeof(key), typeof(+key))
The result is 15 string number. I'm trying to iterate over object values and put some of them into Map object but types compatibility seems unable to achieve. Am I doing something wrong here or object keys are always strings?
Object.keys(obj)
also returns ["15"]
Object keys are always strings. You can see more about it here:
Property names must be strings. This means that non-string objects cannot be used as keys in the object. Any non-string object, including a number, is typecasted into a string via the toString method.
For you to be able to achieve what you want you will need to cast the keys back to integers.

How do I iterate over a dynamic keyed object? [duplicate]

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 6 years ago.
I have a hashed object where the keys are added dynamically through user selections.
I want to iterate over it and extract the values similar to the way I would do if it was simply an array: selections.map(cart => /*do stuff*/).
How can I achieve this?
Use Object.keys()
The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
var array = Object.keys(selections).map(k => selections[k]);
// get all values from the object

Javascript array alllocation [duplicate]

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])

Categories