Код: Выделить всё
const master = [
{key:"Apprentices", label:"Apprentices", children:[
{key:"1", label:"Linda"},
{key:"2", label:"George"}
]},
{key:"Electricians", label:"Electricians", children:[
{key:"3", label:"Kate"},
{key:"4", label:"Diane"}
]}
]
var elements = Array.from(master);
Интересно, если я удалю не дочернее свойство, например
Код: Выделить всё
{key:"Apprentices", label:"Apprentices", children:[
{key:"1", label:"Linda"},
{key:"2", label:"George"}
]}
В моем приложении я не могу использовать главную переменную в качестве CONST, к сожалению, но на jsplayground.dev я могу, но это не так. изменить ситуацию.
Здесь это мой полный код для удаления элемента из children свойства, который в конечном итоге удаляется из обеих переменных:
Код: Выделить всё
const master = [
{key:"Apprentices", label:"Apprentices", children:[
{key:"1", label:"Linda"},
{key:"2", label:"George"}
]},
{key:"Electricians", label:"Electricians", children:[
{key:"3", label:"Kate"},
{key:"4", label:"Diane"}
]}
]
var elements = Array.from(master);
var el = {'name': 3, 'label': 'Kate'}
console.log(elements)
console.log(master) // this is master variable not 'elements' variable
function addRemoveElement(el, addRemove, all) {
if (addRemove === 0) {
// remove
elements.forEach((item) => item.children.forEach((subItem, i) => {
if (subItem.label === el.label) {
item.children.splice(i, 1);
}
}));
}
console.log(elements)
console.log(master) // this is master variable not 'elements' variable
}
addRemoveElement(el, 0)
Код: Выделить всё
const master = [
{key:"Apprentices", label:"Apprentices", children:[
{key:"1", label:"Linda"},
{key:"2", label:"George"}
]},
{key:"Electricians", label:"Electricians", children:[
{key:"3", label:"Kate"},
{key:"4", label:"Diane"}
]}
]
var elements = Array.from(master);
var el = {'key': 'Apprentices', 'label': 'Apprentices'}
console.log(elements)
console.log(master) // this is master variable not 'elements' variable
function addRemoveElement(el, addRemove, all) {
if (addRemove === 0) {
// remove
elements.forEach((item, i) => {
if (item.label === el.label) {
elements.splice(i, 1);
}
});
}
console.log(elements)
console.log(master) // this is master variable not 'elements' variable
}
addRemoveElement(el, 0)
1 — удаляет из обоих:
Код: Выделить всё
(2) [Object, Object]
0: Object
1: Object
key: "Electricians"
label: "Electricians"
children: (2) [Object, Object]
(2) [Object, Object]
0: Object
1: Object
key: "Electricians"
label: "Electricians"
children: (2) [Object, Object]
(2) [Object, Object]
0: Object
1: Object
key: "Electricians"
label: "Electricians"
children: (1) [Object]
(2) [Object, Object]
0: Object
1: Object
key: "Electricians"
label: "Electricians"
children: (1) [Object]
Код: Выделить всё
(2) [Object, Object]
0: Object
1: Object
(2) [Object, Object]
0: Object
1: Object
(1) [Object]
0: Object
(2) [Object, Object]
0: Object
1: Object
Я действительно застрял в этом, поэтому буду очень признателен за любые рекомендации. Спасибо.
Подробнее здесь: https://stackoverflow.com/questions/793 ... ject-array
Мобильная версия