如何查找“items_tolookfor”的索引另一个嵌套数组列表“nested_data”中的项目数组在 JavaScript 中

发布于 2025-01-12 03:49:48 字数 1962 浏览 0 评论 0原文

如何在javascript中的另一个嵌套数组列表“nested_data”中查找“items_tolookfor”项目数组的索引

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)

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

如何在嵌套数组列表“nested_data”中找到“items_tolookfor”的索引 另外,相应的数据位于嵌套列表“nested_data”索引[2][5]中,

我正在尝试以下代码:

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

预期结果是一个控制台日志输出数组,其中包含在nested_data数组对象中找到的items_to_look_for数组对象的匹配数据,即0:[2][5]、1:[2][5]、2:[2][5]、3:[2][5]、4:[2][5]、 5:[][]

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:[][]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与清晨阳光 2025-01-19 03:49:48

您可以这样做:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文