[英]set array based on an array of indexes
I want to set a boolean value in a nested object structure, based on a list of indexes我想根据索引列表在嵌套的 object 结构中设置 boolean 值
The data I want to update looks like this:我要更新的数据如下所示:
let categories = [{
"name": "Cat 1",
"subs": [{
"name": "Sub Cat 1.1",
"subs": [],
"checked": false
},
{
"name": "Sub Cat 1.2",
"subs": [],
"checked": false
}
],
"checked": false
},
{
"name": "Cat 2",
"subs": [],
"checked": false
}
];
The indexes are stored in an array:索引存储在一个数组中:
let categoryIndexs = [0,1]
So based on categoryIndexs
I want to change the checked
property of the object with name
"Sub Cat 1.2" to true
.因此,基于categoryIndexs
我想将name
“Sub Cat 1.2”的 object 的checked
属性更改为true
。
It is like transforming the array [0,1]
to find categories[0].subs[1]
and update its checked
property.这就像转换数组[0,1]
以查找categories[0].subs[1]
并更新其checked
属性。
So far I managed to get the value and edit it separately, but how to change the categories
variable directly?到目前为止,我设法获取了值并分别对其进行了编辑,但是如何直接更改categories
变量呢?
let categoryIndexs = [0,1]
let tmp_array = categories;
for(var i = 0; i < categoryIndexs.length;i++){
if(i == 0){
tmp_array = tmp_array[categoryIndexs[i]]
} else {
tmp_array = tmp_array.subs[categoryIndexs[i]]
}
}
tmp_array.checked = true
console.log(tmp_array)
The solution needs to be dynamic, so that if "Sub Cat 1.2" would have a nested entry in its own subs
array, I can access it using categoryIndexs
like [0,1,0]
解决方案需要是动态的,因此如果“Sub Cat 1.2”在其自己的subs
数组中有一个嵌套条目,我可以使用[0,1,0]
之类的categoryIndexs
访问它
You can iterate the given indexes in a loop and at the same time go deeper in your data structure, like in this function:您可以在循环中迭代给定的索引,同时 go 在您的数据结构中更深,就像在这个 function 中一样:
function setChecked(categories, categoryIndexes) { let obj = null; for (let index of categoryIndexes) { obj = categories[index]; categories = obj.subs; } obj.checked = true; } let categories = [{"name": "Cat 1","subs": [{"name": "Sub Cat 1.1","subs": [],"checked": false},{"name": "Sub Cat 1.2","subs": [],"checked": false}],"checked": false},{"name": "Cat 2","subs": [],"checked": false}]; setChecked(categories, [0,1]); console.log(categories);
You could use reduce
to get the last subs
up until the last subs
and update the checked
property您可以使用reduce
来获取最后一个subs
直到最后一个subs
并更新checked
的属性
let categories = [{"name": "Cat 1","subs": [{"name": "Sub Cat 1.1","subs": [],"checked": false},{"name": "Sub Cat 1.2","subs": [],"checked": false}],"checked": false},{"name": "Cat 2","subs": [],"checked": false}]; let categoryIndexs = [0, 1]; function update(categories, indices) { const last = indices.pop(); // remove the last const redued = indices.reduce((acc, i) => acc[i].subs, categories); redued[last].checked = true; return categories } console.log(update(categories, categoryIndexs))
You could reduce the array by looking to the index of the iteration and take either subs
or the item.您可以通过查看迭代的索引并获取subs
或 item 来减少数组。
const getValue = (subs, indices) => indices.reduce( ({ subs }, i) => subs[i], { subs } ); let categories = [{ name: "Cat 1", subs: [{ name: "Sub Cat 1.1", subs: [], checked: false }, { name: "Sub Cat 1.2", subs: [], checked: false }], checked: false }, { name: "Cat 2", subs: [], checked: false }], indices = [0, 1]; console.log(getValue(categories, indices, 'subs'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.