Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

358
Vistas
How to return indexes of the filtered values, which equal to num?

function fMI(num,arr){
    let digit = arr.toString().split('');
    let realDigit = digit.map(Number);
    console.log("Is RealDigit an array: ",Array.isArray(realDigit));
    console.log(realDigit.filter((v,i,a) => (v==num)? i:''));
}

fMI(1,[1, 11, 34, 52, 61]);

// Create a function that takes a number and an array of numbers as a parameter // and returns the indices of the numbers of the array which contain the given number // or returns an empty list (if the number is not part of any of the numbers in the array)

Sample output: // console.log(findMatchingIndexes(1, [1, 11, 34, 52, 61])); // should print: [0, 1, 4]

I am struggling to return the indexes of the values from an array. The code I have written so far takes one number and compares it to the array which the function takes in. Now, I am at the phase where I want to to return the indexes where the array values equal to the number taken by the function for comparison.

How can I make this work, cause after the filter method, I get only 3 times the number 1, which is not right.

function fMI(num:number,arr:number[]){
       let digit = arr.toString().split('');
       let realDigit = digit.map(Number);
       console.log("Is RealDigit an array: ",Array.isArray(realDigit));
       console.log(realDigit.filter((v,i,a) => (v==num)? i:''));
}

fMI(1,[1, 11, 34, 52, 61]);
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use a traditional for loop:

function fMI(num, arr) {
  let result = [];
  let numArr = arr.map(item => Number(item));
  for (var i = 0; i < numArr.length; i++) {
    if (numArr[i] == num) {
      result.push(i);
    }
  }
  console.log(result);
}

fMI(1, [1, 11, 34, 52, 61]);

about 4 years ago · Juan Pablo Isaza Denunciar

0

New answer

You can use the string method includes like so:

function fMI(num: number, arr: number[]) {
  const indexes: number[] = [];
  arr.forEach((value, index) => {
    if (value.toString().includes(num.toString())) {
      indexes.push(index);
    }
  })
  return indexes;
}

fMI(1, [1, 11, 34, 52, 61]); // [0, 1, 4]

Old answer

You can use forEach with a second argument that is the element index. Note that you are specifying arr as an array of number, so there's no need to convert them to numbers again.

function fMI(num: number, arr: number[]) {
  const indexes: number[] = [];
  arr.forEach((value, index) => {
    if (value === num) {
      indexes.push(index);
    }
  })
  return indexes;
}

The 4 lines you have written doesn't help with the logic, that's why I removed it. If you want to know why, leave a comment on this answer and I will reply your questions.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here's an interesting take:

function fMI(num,arr) {
       return arr
              .map((n) => n.toString().split('')
              .map((m) => parseInt(m)))
              .map((o, i) => o.some((p) => p === num) ? i : null)
              .filter((f) => f !== null);
}

const result = fMI(1,[1, 11, 34, 52, 61]);

console.log(result);

First we go through all the numbers in the array, convert them to strings and split them to replace the original numbers with arrays of digits. Then we run those digit arrays through parseInt it convert them to number arrays. Finally we use .some to check if any of the digits in the digit array is num and return an array of indexes or nulls which we run through a filter that removes the nulls and finally we return an array of the indexes for where any of the digits of the number is the same as num.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda