如何在 javascript 中的另一个嵌套数组列表 'nested_data' 中查找 'items_tolookfor' 项数组的索引
How to find index of 'items_tolookfor' array of items in another nested Array List 'nested_data' in javascript
如何在 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' index[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:[][]
您可以按照以下方式进行操作:
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));
}
如何在 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' index[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:[][]
您可以按照以下方式进行操作:
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));
}