How to get an index in all cases of the same value?

Viewed 63

I have two arrays where one is to check the indexes and one is with values to check.

    var array = [1200.0, 1000.0, 1000.0, 200.0, 1000.0, 1800.0, 350.0, 336.0, 110.0, 1900.0, 130.0];
    var check = [1200.0, 1000.0, 1000.0, 200.0, 110.0, 130.0];
    var indeks = [];


for(var i = 0; i < check.length; i++){
     // Logger.log(check[i]);
     //indeks.push(array.indexOf(check[i]));

     function getAllIndexes(arr, val) {
    var indexes = [], i = -1;
    while ((i = arr.indexOf(val, i+1)) != -1){
        indexes.push(i);
    }
    return indexes;
}
indeks.push(getAllIndexes(array, check[i]));
    }
    Logger.log(indeks);

the output I am getting now is: [[0.0], [1.0, 2.0, 4.0], [1.0, 2.0, 4.0], [3.0], [8.0], [10.0]]

I want to get output that looks like this: [0, 1, 2, 3, 8, 10]

I found getAllIndexes in another question here but somehow it doesn't work well and I am not sure where I am getting wrong. When I use only indeks.push(array.indexOf(check[i])) I am getting this: [0.0, 1.0, 1.0, 3.0, 8.0, 10.0] which is pretty close to result I am looking for

If anyone know the solution I would be very gratefull

2 Answers

var array = [1200.0, 1000.0, 1000.0, 200.0, 1000.0, 1800.0, 350.0, 336.0, 110.0, 1900.0, 130.0];
var check = [1200.0, 1000.0, 1000.0, 200.0, 110.0, 130.0];

function getUniqueIndexes(array, check) {
  // copy array to prevent mutation
  var arr = array.slice()

  // loop through `check`
  const indeks = check.reduce((acc, cur) => {
    // find index of the current value
    var i = arr.indexOf(cur)
    if (i !== -1) {
      // erase the value to prevent further matches
      arr[i] = null
      // return the acumulator with the new index for each loop
      return [...acc, i]
    }
  }, [])

  return indeks
}

const indeks = getUniqueIndexes(array, check)
console.log(indeks)
// result should be [0, 1, 2, 3, 8, 10]

That's it ;)

I'm not sure if this code is ES6 or not, maybe you will need to transpile it before use

There are a few problems in your code, like declaring the function inside the loop. When you used indeks.push(array.indexOf(check[i])), you were super close to the answer. The reason it returned indeks as [0.0, 1.0, 1.0, 3.0, 8.0, 10.0] is because the function finds the index of val in the array, starting from the beginning. Therefore, if you have duplicate elements (in this case, yes), it only finds the first occurrence. I solved it by looking at this MDN reference. It says that indexOf also has an optional parameter called fromIndex. This is where you start searching the array. Using a nums array which I push to every time in the function. I take the length of nums, and pass it in as the fromIndex param. Hope that made sense, and here's the code:

var array = [1200.0, 1000.0, 1000.0, 200.0, 1000.0, 1800.0, 350.0, 336.0, 110.0, 1900.0, 130.0];
var check = [1200.0, 1000.0, 1000.0, 200.0, 110.0, 130.0];
var indeks = []; 
var nums = [];
var indexes = []; // Top Level (for scope)

    function getAllIndexes(arr, val) {
      if (arr.indexOf(val, nums.length) > -1) {
        indexes.push(arr.indexOf(val, nums.length));
        nums.push("num")
      }
    }        

    for (var i = 0; i < check.length; i++) {
      indeks.push(getAllIndexes(array, check[i]));
    }  
    console.log(indexes)
Related