Home > Software engineering >  How to return indexes of the filtered values, which equal to num?
How to return indexes of the filtered values, which equal to num?

Time:06-12

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

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

You can inject this method to your array prototype like this:

Array.prototype.filterMapIndex = function(expression){
  const indexs = []

    this.filter((newval,index,full)=>{
      if(expression(newval,index,full)) indexs.push(index)
    })
    return indexs
}

next, you can use it simply by calling method :

let myArray = [1,2,3,4,0,3];

const indexes = myArray.filterMapIndex((val)=>{ return val>0 })

Array.prototype.filterMapIndex = function(expression){
  const indexs = []

    this.filter((newval,index,full)=>{
      if(expression(newval,index,full)) indexs.push(index)
    })
    return indexs
}

let myArray = [1,2,3,4,0,3];

const indexes = myArray.filterMapIndex((val)=>{ return val>0 })
console.log(indexes)

  • Related