![](/img/trans.png)
[英]Vuejs Loop trough all children and execute a function for each iteration
[英]Javascript/jQuery - For each array iteration trough all multidimensional keys
我有一个具有以下结构的多维数组:
[13] => Array
(
[id] => 51
[text] => Corporate
[seltext] => Corporate
[parent_id] => 0
[object_type] => folder
[corefilename] =>
[userUNIQUE] =>
[nodes] => Array
(
[0] => Array
(
[id] => 50
[text] => Due diligence
[seltext] => Due diligence
[parent_id] => 51
[object_type] => folder
[corefilename] =>
[userUNIQUE] =>
)
[1] => Array
(
[id] => 2
[text] => Drafts
[seltext] => Drafts
[parent_id] => 51
[object_type] => folder
[corefilename] =>
[userUNIQUE] =>
[nodes] => Array
(
[0] => Array
(
[id] => 6
[text] => de
[seltext] => Decisions [parent_id] => 2
[object_type] => folder
[corefilename] =>
[userUNIQUE] =>
[nodes] => Array
(
[0] => Array
(
[id] => 44
[text] =>
[parent_id] => 6
[object_type] => file
[corefilename] => f-8TYO89KHTK1BNV4QMMAW6GHEEDRREECP1VDV4Y6VLXWM7XS97V4LCUWXJFM6E61VXF
[userUNIQUE] =>
我想获取一直填充到数组中的所有“id”值 - 节点。 我试过迭代,但我唯一能达到的水平是第一个。
这是我试图运行但没有成功的 function:
var childs=$('#tree').treeview('getSelected', loc);
function getValuesByKey(object, key) {
var values = [];
recursiveFx(object);
function recursiveFx(object) {
for (var property in object) {
if (object.hasOwnProperty(property)) {
if (typeof object[property] == "object") {
recursiveFx(object[property]);
} else {
try {
if (isDefined(object[key])) {
console.log('value:',object[key]);
values.push(object[key]);
}
} catch (e) {
}
}
}
}
}
return values;
}
getValuesByKey(childs, 'id');
我究竟做错了什么?
据我从您的数据中看到,您首先遍历数组,而不是通过 object。 object 也可以使用相同的解决方案,但是,必须对属性和数组元素进行迭代,除非您知道某些特定属性将始终包含所有对象的数组。
这只是一个示例数据,以节省时间:
var data = [
{"id": 50},
{
"id": 35,
"nodes": [
{"id": 3},
{"id": 7},
{
"id": 9,
"nodes": [
{"id": 0},
{"id": 1}
]
},
]
},
];
接下来是您获取 object 中的属性,并检查它是否具有该属性,这将始终为真。
我建议您使用Array.isArray检查特定字段是否为数组,并将新数组提供给 recursiveFx。
function getValuesByKey(arr, key) {
var values = [];
recursiveFx(arr);
function recursiveFx(arr) {
for (let el in arr) {
object = arr[el];
for(let property in object){
if(property === key){
values.push(object[property]);
} else if(Array.isArray(object[property])){
recursiveFx(object[property])
}
}
}
}
return values;
}
console.log(getValuesByKey(data, 'id'));
我认为您的数据是“数组”? 但var property in object
这是一个简单的递归 function 来解决这类问题。
let test_data =
[{
"id": 1,
"somekey": "someval1",
"nodes": [{
"id": 2,
"somekey": "someval2",
"nodes": [{
"id": 4,
"somekey": "someval4"
}, {
"id": 5,
"somekey": "someval5",
"nodes": [{
"id": 6,
"somekey": "someval6"
}]
}]
}]
},
{
"id": 3,
"somekey": "someval3"
}
]
和递归 function:
const nodes_key = "nodes";
const key = "id";
let values = [];
function getValuesByKey(ary, key, nodes_key){
for ( object of ary ){
if (object.hasOwnProperty(key)) {
values.push(object[key]) ;
}
if ( object.hasOwnProperty(nodes_key) ){
getValuesByKey(object[nodes_key], key, nodes_key)
} // if it has child-nodes, just do same as above.. (pushes the value of key)
} // retrive each object in an Array
}
Output:
getValuesByKey(test_data, key, nodes_key)
> values
[ 1, 2, 4, 5, 6, 3 ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.