Second and third line create property.
Fourth and fifth line create value.
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.
How can I get only the properties?
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.
const object = {};
object['test1'] = 'property 1';
object['test2'] = 'property 1';
object[0] = 'value 1';
object[1] = 'value 2';
const logger = ([name, value]) => console.log(`${name} is ${value}`);
Object.entries(object).forEach(logger);
console.log("-------");
Object.entries(object).filter(([name]) => !/^\d+$/.test(name)).forEach(logger);