Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
javascript indexOf elemento que contiene la clase

Tengo una matriz de elementos de la misma clase, ¿cómo puedo buscar indexOf con solo un elemento de la clase?

 class Dhash { constructor(Dlable, tophash) { this.Dlable = Dlable; this.tophash = tophash; } } let listohashs = []; listohashs[0] = new Dhash(0x001003, 0xfffffffffffffffffffffffffffffffffa); listohashs[1] = new Dhash(0x011003, 0xfffffffffffffffffffffffffffffffffb); listohashs[2] = new Dhash(0x021003, 0xfffffffffffffffffffffffffffffffffc); console.log(listohashs.indexOf(0x001003)); // <-- here be the problem

Necesito que devuelva 0 para este ejemplo, ya que coincide con listohashs[0].dlable esa manera puedo obtener el valor tophash

Lo he intentado: console.log(listohashs.indexOf(0x001003)); y poner .dlable en cualquier lugar que se me ocurra.

¿Puedo buscar con un comodín en uno de los lugares del elemento? es decir, donde * coincidirá con cualquier cosa

 searchohash = new Dhash(0x001003, *); console.log(listohashs.indexOf(searchohash));

¿Es json lo que debería usar? soy nuevo en js y comencé a usar json hace unos días

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

indexOf solo funciona si el argumento es un elemento real de la matriz, no solo una propiedad de la misma.

Use findIndex() para encontrar un elemento usando una función que realizará la comparación adecuada.

 class Dhash { constructor(Dlable, tophash) { this.Dlable = Dlable; this.tophash = tophash; } } let listohashs = []; listohashs[0] = new Dhash(0x001003, 0xfffffffffffffffffffffffffffffffffa); listohashs[1] = new Dhash(0x011003, 0xfffffffffffffffffffffffffffffffffb); listohashs[2] = new Dhash(0x021003, 0xfffffffffffffffffffffffffffffffffc); console.log(listohashs.findIndex(h => h.Dlable == 0x001003));

about 4 years ago · Juan Pablo Isaza Report

0

El método indexOf() devuelve el primer índice en el que se puede encontrar un elemento dado en la matriz, o -1 si no está presente. - MDN

Está buscando el elemento cuyo valor es 0x001003 pero listohashs es una matriz de objetos. Entonces, está comparando el objeto con 0x001003 , que no será igual, por lo que devolverá -1 .

Puede usar findindex aquí. Tiene que encontrar el index del objeto cuyo valor de propiedad Dlable es 0x001003

 class Dhash { constructor(Dlable, tophash) { this.Dlable = Dlable; this.tophash = tophash; } } let listohashs = []; listohashs[0] = new Dhash(0x001003, 0xfffffffffffffffffffffffffffffffffa); listohashs[1] = new Dhash(0x011003, 0xfffffffffffffffffffffffffffffffffb); listohashs[2] = new Dhash(0x021003, 0xfffffffffffffffffffffffffffffffffc); console.log(listohashs.findIndex((o) => o.Dlable === 0x001003)); // <-- here be the problem

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!