弄出來了,自己答吧~
/**
* 查詢多維陣列值並返回索引索引目錄
* @param {object} arr 要查詢的陣列
* @param {string} val 要查詢的值(必須唯一)
* @param {string} key 要查詢的值對應的鍵 [可選]
* @return {array} index 返回索引路徑
*/
const search_obj_key = function (arr, val, position) {
let idx_path = "";
function _foreach (arr, val, position) {
var temp = "";
arr.forEach((item, index) => {
temp = position ? position + "," + index : index;
_findValue(item, index, temp);
if (Object.prototype.toString.call(item) == "[object Object]") {
let _array = Object.values(item);
for(var arr_idx in _array){
_findValue(_array[arr_idx], index, temp);
}
if (Object.prototype.toString.call(item) == "[object Array]") {
_foreach(item, val, temp);
})
function _findValue (item, index, temp) {
if (item === val) {
idx_path = temp;
return;
} else if (Object.prototype.toString.call(item) == "[object Array]") {
} else if (Object.prototype.toString.call(item) == "[object Object]") {
for(var arr_idx in Object.values(item)){
_foreach(arr, val, position);
if(!idx_path){
return false;
return idx_path.toString().split(",").map((v)=>{return parseInt(v)});
弄出來了,自己答吧~
/**
* 查詢多維陣列值並返回索引索引目錄
* @param {object} arr 要查詢的陣列
* @param {string} val 要查詢的值(必須唯一)
* @param {string} key 要查詢的值對應的鍵 [可選]
* @return {array} index 返回索引路徑
*/
const search_obj_key = function (arr, val, position) {
let idx_path = "";
function _foreach (arr, val, position) {
var temp = "";
arr.forEach((item, index) => {
temp = position ? position + "," + index : index;
_findValue(item, index, temp);
if (Object.prototype.toString.call(item) == "[object Object]") {
let _array = Object.values(item);
for(var arr_idx in _array){
_findValue(_array[arr_idx], index, temp);
}
}
if (Object.prototype.toString.call(item) == "[object Array]") {
_foreach(item, val, temp);
}
})
}
function _findValue (item, index, temp) {
if (item === val) {
idx_path = temp;
return;
} else if (Object.prototype.toString.call(item) == "[object Array]") {
_foreach(item, val, temp);
} else if (Object.prototype.toString.call(item) == "[object Object]") {
let _array = Object.values(item);
for(var arr_idx in Object.values(item)){
_findValue(_array[arr_idx], index, temp);
}
}
}
_foreach(arr, val, position);
if(!idx_path){
return false;
}
return idx_path.toString().split(",").map((v)=>{return parseInt(v)});
}