Length of an array in JavaScript

1

I have to do the following exercise:

  

Extend the given class to implement a count () function that prints the number of elements contained in the "value" property in the console. (Do not modify the given constructor)

     

Instance the class and call the new count function ().

// No modificar este constructor.
function Clase (elem)
{
  this.valor = [];
  this.valor['elem1'] = 1;
  this.valor['elem2'] = 2;
  this.valor['elem3'] = 3;
  this.valor['elem4'] = elem;
}

Given this exercise, what I did was:

Clase.prototype.metodo()
{
  var a = this.valor.length;
  return a;
}

var miClase = new Clase(4);

console.log(miClase.metodo())

But the array length gives me 0; I do not know what I was wrong about, where is the problem and how can I solve it?

    
asked by PCH 03.07.2017 в 20:37
source

3 answers

4

Since valor is an object, I recommend you count the number of properties in the following way

 function Clase (elem) {
     this.valor = [];
     this.valor['elem1'] = 1;
     this.valor['elem2'] = 2;
     this.valor['elem3'] = 3;
     this.valor['elem4'] = elem;
 }

 Clase.prototype.metodo = function() {
     var a = Object.keys(this.valor).length;
     return a;
 }

 var miClase = new Clase(4);

 console.log(miClase.metodo())
Using Object.keys(obj) returns an array with the keys of the object. Which allows you to get your length     
answered by 03.07.2017 / 20:54
source
1

The problem is that you are not adding the elements to the array, but that you are adding them as properties of the array, and these properties are not counted in length , they are normal properties of an object.

Where you say

this.valor['elem1'] = 1;
this.valor['elem2'] = 2;
this.valor['elem3'] = 3;
this.valor['elem4'] = elem;

should be

this.push(1);
this.push(2);
this.push(3);
this.push(elem);

so that the property% co_of% value has a value other than length .

But as it says Do not modify the constructor it will have to do something different. This depends on the interpretation of the slogan.

If it is understood that "the number of elements that contain the property value" as the number of elements that have the 0 value or when properties have the array .

In that case, wanting the number of elements in the array, as well as this is fine, does not require changes.

If you want objeto array retrone the amount of properties you can do:

Clase.prototype.Contar = function() {
  return Object.keys(this.valor).length;
}

Which counts the amount of properties (not elements) of the object.

salu2

    
answered by 03.07.2017 в 20:48
1

You have to take into account one thing: what you have is not an array but an associative array (or an object). That is, a group of key-value couples. That's why length will not work. You can use a method like the one explained in this English site response , counting the keys in the object / associative array:

// No modificar este constructor.
function Clase (elem)
{
  this.valor = [];
  this.valor['elem1'] = 1;
  this.valor['elem2'] = 2;
  this.valor['elem3'] = 3;
  this.valor['elem4'] = elem;
}

Clase.prototype.metodo = function()
{
   var numElementos = Object.keys(this.valor).length;
   return numElementos;
}

var miClase = new Clase(4);

console.log(miClase.metodo())
    
answered by 03.07.2017 в 20:57