How to get JavaScript hash table count? [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Length of Javascript Associative Array
hash_table = {a: 131, b: 222, c:313}
The length method is not working of course since it will be confused with a key.
So how do I do it?

Object.keys will return all the keys in the object as a list, then use length to get the length.
example:
Object.keys(hash_table).length
NOTE that this is ECMA 5 and may not be available in some browsers. see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys for full document.

var count = 0;
for ( property in hash_table ) count++;

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.

Why does a "for ... in" loop—for (var/let/const A in B)—make A a string in vanilla ES6? [duplicate]

This question already has answers here:
javascript for loop counter coming out as string [duplicate]
(3 answers)
Closed 3 years ago.
This is in Chromium 78:
for (var i in [1,3,5]) console.log(i+1)
Now, I expected for (var i in [1,3,5]) console.log(i+1) to output 1, 2, 3, because i should be an index value. I know the MDN docs mention that the order may come out strangely in this case, but why the type conversion?
i is not the index, i is the property key of the array object. Property keys are always strings.

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

How to check if a list contains a string in javascript? [duplicate]

This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 6 years ago.
How can I test if the string "Orange" without quotes, exists in a list such as the one below, using javascript?
var suggestionList = ["Apple", "Orange", "Kiwi"];
indexOf() is the default to use in such cases.
if( suggestionList.indexOf("Orange") > -1 ) {
console.log("success");
}
.indexOf() returns the position in the array (or string) of the found element. When no elements are found it returns -1.
Things to consider:
It will return 0 if the first object matches, so you cannot just cast to boolean.
Since es2016 you can use suggestionList.includes('Kiwi'); which will return a boolean directly. (Does not work in internet explorer, but works in IEdge)
includes polyfill for older browsers: https://babeljs.io/docs/usage/polyfill/

Getting the length of a 'named' array? [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 9 years ago.
I'm not sure what they are called, but what I mean is this:
array["water"] = 50;
array["fire"] = 30;
length should be 2 here
how can I see how many attributes I have in the array? array.length doesn't work =( I've been trying all kinds of things and I feel like I'm missing something really simple here..
Thank you for your help
You could use Object.keys() to obtain an array of keys, then count them:
Object.keys(array).length
Or, if you're targeting ECMAScript 3 or otherwise don't have Object.keys(), then you can count the keys manually:
var length = 0;
for (var key in array) {
if (array.hasOwnProperty(key)) {
++length;
}
}
There are a few edge cases with this approach though, depending on the browsers you're targeting, so using Mozilla's polyfill for Object.keys() instead might be a good idea.

Categories