Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]);

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading