Skip to content

How to find index of ‘items_tolookfor’ array of items in another nested Array List ‘nested_data’ in javascript

How to find index of ‘items_tolookfor’ array of items in another nested Array List ‘nested_data’ in javascript

const items_tolookfor = []
console.log(items_tolookfor) is as below

0: "urn:adsk.wipprod:fs.folder:co.uqbu12FFQZO6y7GxUs7cKA"
1: "urn:adsk.wipprod:fs.folder:co.uqbu12FFQZO6y7GxUs7cKA"
2: "urn:adsk.wipprod:fs.folder:co.uqbu12FFQZO6y7GxUs7cKA"
3: "urn:adsk.wipprod:fs.folder:co.uqbu12FFQZO6y7GxUs7cKA"
4: "urn:adsk.wipprod:fs.folder:co.uqbu12FFQZO6y7GxUs7cKA"
5: "urn:adsk.wipprod:fs.folder:co._rdgx-sJT36zVaGNILbvvQ"
length: 6
lastIndex: (...)
lastItem: (...)
[[Prototype]]

//////////////////////////////////////////////////////////
const nested_data = []
console.log(nested_data) is as follows:

0: Array(0)
length: 0
lastIndex: (...)
lastItem: (...)
[[Prototype]]: Array(0)
1: Array(0)
length: 0
lastIndex: (...)
lastItem: (...)
[[Prototype]]: Array(0)
2: Array(5)
0: "urn:adsk.wipprod:fs.folder:co.yoqKNIJMTmWvdxFIYGk8sg"
1: "urn:adsk.wipprod:fs.folder:co.eGqoz0IlTriGsYLTKbIrIA"
2: "urn:adsk.wipprod:fs.folder:co.O4tlfhMhSACS81dsygYJSw"
3: "urn:adsk.wipprod:fs.folder:co.FBQOhzXkSa6upov-iay5EQ"
4: "urn:adsk.wipprod:fs.folder:co.nXNGKsqTQUGr_hcTXy6U5g"
5: "urn:adsk.wipprod:fs.folder:co.uqbu12FFQZO6y7GxUs7cKA"
length: 5
lastIndex: (...)
lastItem: (...)
[[Prototype]]: Array(0)

///////////////////////////////////////////

How would I find indices for ‘items_tolookfor’ in the nested array list ‘nested_data’ Also, the corresponding data lies in the nested list ‘nested_data’ index[2][5]

I was trying this code:

        console.log(items_tolookfor);
        console.log(nested_data);
        const found_indices = [];

        for (const assdata in nested_data) {
          const temp = [];
          for (const fldr_data in items_tolookfor) {
            const temp1 = assdata.indexOf(fldr_data);
            temp.push(temp1);
          }
          found_indices.push(temp);
        }

Expected result to be a console log output array with matching data for items_to_look_for array object found in nested_data array object, which is 0: [2][5], 1:[2][5], 2:[2][5], 3: [2][5], 4: [2][5], 5:[][]

Answer

Here’s how you could go about that:

const items_to_look_for = [
    'urn:adsk.wipprod:fs.folder:co.uqbu12FFQZO6y7GxUs7cKA',
    'urn:adsk.wipprod:fs.folder:co.uqbu12FFQZO6y7GxUs7cKA',
    'urn:adsk.wipprod:fs.folder:co.uqbu12FFQZO6y7GxUs7cKA',
    'urn:adsk.wipprod:fs.folder:co.uqbu12FFQZO6y7GxUs7cKA',
    'urn:adsk.wipprod:fs.folder:co.uqbu12FFQZO6y7GxUs7cKA',
    'urn:adsk.wipprod:fs.folder:co._rdgx-sJT36zVaGNILbvvQ'
];

const nested_data = [
    [],
    [],
    [
        'urn:adsk.wipprod:fs.folder:co.yoqKNIJMTmWvdxFIYGk8sg',
        'urn:adsk.wipprod:fs.folder:co.eGqoz0IlTriGsYLTKbIrIA',
        'urn:adsk.wipprod:fs.folder:co.O4tlfhMhSACS81dsygYJSw',
        'urn:adsk.wipprod:fs.folder:co.FBQOhzXkSa6upov-iay5EQ',
        'urn:adsk.wipprod:fs.folder:co.nXNGKsqTQUGr_hcTXy6U5g',
        'urn:adsk.wipprod:fs.folder:co.uqbu12FFQZO6y7GxUs7cKA'
    ]
];

function findIndexRecursive(item, data) {
    for (let i = 0, len = data.length; i < len; i++) {
        if (data[i] === item) {
            return [i];
        } else if (data[i] instanceof Array) {
            const nestedIndex = findIndexRecursive(item, data[i]);
            if (nestedIndex) {
                return [i].concat(nestedIndex);
            }
        }
    }
    return null;
}

for (const item of items_to_look_for) {
    console.log(findIndexRecursive(item, nested_data));
}