0

I have the following code:

var member_data_list = [
  ["adrian", "test", Number(9)],
  ["supler", "test", Number(2.3)],
  ["franz", "test", Number(2.5)],
  ["drucker", "test", Number(9.6)],
];

if (exists(member_data_list, "franz")) {
     //HERE I NEED TO GET THE INDEX OF MEMBER_DATA_LIST[?]
     //EXAMPLE: if (exists(member_data_list, "franz")) => RESULT => 2 (MEMBER_DATA_LIST[2])
     console.log("NAME IS IN ARRAY AT INDEX " + "?");
}
        
function exists(arr, search) {
   return arr.some(row => row.includes(search));
}

How to get the index of the name in this array?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Marcus Derem
  • 95
  • 1
  • 8
  • You'll have to use a loop like `forEach` which returns the index of the loop. `Array.some` is a good solution but it only returns a boolean, so a manual loop may be required here to grab the index – Sterling Archer Feb 20 '20 at 14:56
  • 2
    You can use `findIndex` with an appropriate function... – Heretic Monkey Feb 20 '20 at 14:58
  • 1
    Does this answer your question? [indexOf method in an object array?](https://stackoverflow.com/questions/8668174/indexof-method-in-an-object-array) – Heretic Monkey Feb 20 '20 at 15:00

5 Answers5

1

One solution is by getting the element through .find(), and then use the .indexOf(). for example:

var member_data_list = [
  ["adrian", "test", Number(9)],
  ["supler", "test", Number(2.3)],
  ["franz", "test", Number(2.5)],
  ["drucker", "test", Number(9.6)],
];

if (exists(member_data_list, "franz")) {
    var found = member_data_list.find(row => row[0] === "franz")
    var index = member_data_list.indexOf(found)
    
    // if the element is exists then index's value will be greater than -1
    if (index > -1) {
      console.log("NAME IS IN ARRAY AT INDEX " + index)
    }
}
        
function exists(arr, search) {
   return arr.some(row => row.includes(search));
}
novalagung
  • 10,905
  • 4
  • 58
  • 82
0

You could create another function to get the index looping the array and using includes:

var member_data_list = [
  ["adrian", "test", Number(9)],
  ["supler", "test", Number(2.3)],
  ["franz", "test", Number(2.5)],
  ["drucker", "test", Number(9.6)],
];

if (exists(member_data_list, "franz")) {
     var index = findIndex(member_data_list, 'franz'); 
     console.log("NAME IS IN ARRAY AT INDEX " + index);
}
        
function exists(arr, search) {
   return arr.some(row => row.includes(search));
}

function findIndex(array, value) {
    for (var i = 0; i < array.length; i += 1) {
        if (array[i].includes(value)) {
            return i;
        }
    }
    return -1;
}
jeprubio
  • 17,312
  • 5
  • 45
  • 56
0

You can iterate the array and return the index of the nested array. In order to find if that name is available in any child array use includes or indexOf. Also suppose the array contains two child array with some content then the higher index will be returned

var member_data_list = [
  ["adrian", "test", Number(9)],
  ["supler", "test", Number(2.3)],
  ["franz", "test", Number(2.5)],
  ["drucker", "test", Number(9.6)],
];

function exists(arr, name) {
  let ind;
  arr.forEach((item, index) => {
    if (item.includes(name.toLowerCase())) {
      ind = index
    }
  })
  return ind;
}
let isExist = exists(member_data_list, "franz")
if (isExist) {
  console.log("NAME IS IN ARRAY AT INDEX " + isExist);
}
brk
  • 48,835
  • 10
  • 56
  • 78
0

You could also use Array.prototype.findIndex() (not working on Internet Explorer)

var member_data_list = [
  ["adrian", "test", Number(9)],
  ["supler", "test", Number(2.3)],
  ["franz", "test", Number(2.5)],
  ["drucker", "test", Number(9.6)],
];

if (exists(member_data_list, "franz")) {
     var index = findIndex(member_data_list, 'franz'); 
     console.log("NAME IS IN ARRAY AT INDEX " + index);
}
        
function exists(arr, search) {
   return arr.some(row => row.includes(search));
}

function findIndex(arr, search) {
   return arr.findIndex(row => row.includes(search));
}
jeprubio
  • 17,312
  • 5
  • 45
  • 56
0

Single line:

var i = member_data_list.findIndex(item => item[0] ==="franz");

Run this online:

var member_data_list = [
  ["adrian", "test", Number(9)],
  ["supler", "test", Number(2.3)],
  ["franz", "test", Number(2.5)],
  ["drucker", "test", Number(9.6)],
];

console.log(member_data_list.findIndex(item => item[0] ==="franz"));
David
  • 15,894
  • 22
  • 55
  • 66