From the answer of Why does a string index in an array not increase the ‘length’?, I found out that arrays can have both value and properties.
For example in the following code:
var array = [];
array['test1'] = 'property 1';
array['test2'] = 'property 1';
array[0] = 'value 1';
array[1] = 'value 2';
Second and third line create property.
Fourth and fifth line create value.
I can get the values using:
array.forEach((value) => {
console.log(value);
});
How can I get only the properties?
3
Answers
You can use it
No. All of those lines create a property and assigns a value to it.
The only difference is that some of those property names are integers.
If you want to use properties that are not numbers then do not use an array.
The purpose of an array is to store a set of values in an order. It does this using numeric property names. It has lots of special features specifically for handling numeric property names.
Use a regular object instead. You can convert it to an array you are iterate over with
Object.entries
, and filter out selected properties based on their name if you like.You can use a couple of conditions to filter out those you don’t need:
Try it: