Update a value in a nested array while maintaining original indexes ES6 [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
JavaScript: Setting Nested object value by ID
4 answers
How to find a node in a tree with JavaScript
9 answers
I want to update a value in a nested array and return the updated array. This should all be in ES6. I may have over 2000 children and I have only the id from the object that should be updated.
My array looks like:
let data = [{
title: 'Name 1',
key: '0-0',
children: [{
title: 'Name 1-1',
key: '0-0-0',
id: 4,
children: [{
title: 'Name 1-3',
key: '0-0-0-0',
id: 3,
visibility: false,
children: [{
title: 'Name 1-4',
key: '0-0-0-0',
id: 34,
visibility: false // this need to be updated
}]
},
{
title: 'Name 2-1',
key: '0-0-1',
id: 1,
visibility: false
}
]
}]
}];
Thanks for help.
javascript ecmascript-6
marked as duplicate by trincot
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
This question already has an answer here:
JavaScript: Setting Nested object value by ID
4 answers
How to find a node in a tree with JavaScript
9 answers
I want to update a value in a nested array and return the updated array. This should all be in ES6. I may have over 2000 children and I have only the id from the object that should be updated.
My array looks like:
let data = [{
title: 'Name 1',
key: '0-0',
children: [{
title: 'Name 1-1',
key: '0-0-0',
id: 4,
children: [{
title: 'Name 1-3',
key: '0-0-0-0',
id: 3,
visibility: false,
children: [{
title: 'Name 1-4',
key: '0-0-0-0',
id: 34,
visibility: false // this need to be updated
}]
},
{
title: 'Name 2-1',
key: '0-0-1',
id: 1,
visibility: false
}
]
}]
}];
Thanks for help.
javascript ecmascript-6
marked as duplicate by trincot
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
do you want to updatte or to get a whole new data structure with the updated item? btw, what have you tried?
– Nina Scholz
yesterday
you need something like map and then a rekursive function over all childreans
– Tobias K
yesterday
Did you look around? This question must have been asked many times before.
– trincot
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
JavaScript: Setting Nested object value by ID
4 answers
How to find a node in a tree with JavaScript
9 answers
I want to update a value in a nested array and return the updated array. This should all be in ES6. I may have over 2000 children and I have only the id from the object that should be updated.
My array looks like:
let data = [{
title: 'Name 1',
key: '0-0',
children: [{
title: 'Name 1-1',
key: '0-0-0',
id: 4,
children: [{
title: 'Name 1-3',
key: '0-0-0-0',
id: 3,
visibility: false,
children: [{
title: 'Name 1-4',
key: '0-0-0-0',
id: 34,
visibility: false // this need to be updated
}]
},
{
title: 'Name 2-1',
key: '0-0-1',
id: 1,
visibility: false
}
]
}]
}];
Thanks for help.
javascript ecmascript-6
This question already has an answer here:
JavaScript: Setting Nested object value by ID
4 answers
How to find a node in a tree with JavaScript
9 answers
I want to update a value in a nested array and return the updated array. This should all be in ES6. I may have over 2000 children and I have only the id from the object that should be updated.
My array looks like:
let data = [{
title: 'Name 1',
key: '0-0',
children: [{
title: 'Name 1-1',
key: '0-0-0',
id: 4,
children: [{
title: 'Name 1-3',
key: '0-0-0-0',
id: 3,
visibility: false,
children: [{
title: 'Name 1-4',
key: '0-0-0-0',
id: 34,
visibility: false // this need to be updated
}]
},
{
title: 'Name 2-1',
key: '0-0-1',
id: 1,
visibility: false
}
]
}]
}];
Thanks for help.
This question already has an answer here:
JavaScript: Setting Nested object value by ID
4 answers
How to find a node in a tree with JavaScript
9 answers
javascript ecmascript-6
javascript ecmascript-6
edited yesterday
Josh
50036
50036
asked yesterday
GoranR
194
194
marked as duplicate by trincot
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by trincot
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
do you want to updatte or to get a whole new data structure with the updated item? btw, what have you tried?
– Nina Scholz
yesterday
you need something like map and then a rekursive function over all childreans
– Tobias K
yesterday
Did you look around? This question must have been asked many times before.
– trincot
yesterday
add a comment |
do you want to updatte or to get a whole new data structure with the updated item? btw, what have you tried?
– Nina Scholz
yesterday
you need something like map and then a rekursive function over all childreans
– Tobias K
yesterday
Did you look around? This question must have been asked many times before.
– trincot
yesterday
do you want to updatte or to get a whole new data structure with the updated item? btw, what have you tried?
– Nina Scholz
yesterday
do you want to updatte or to get a whole new data structure with the updated item? btw, what have you tried?
– Nina Scholz
yesterday
you need something like map and then a rekursive function over all childreans
– Tobias K
yesterday
you need something like map and then a rekursive function over all childreans
– Tobias K
yesterday
Did you look around? This question must have been asked many times before.
– trincot
yesterday
Did you look around? This question must have been asked many times before.
– trincot
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You can do something like this via a recursive function:
let data = [{ title: 'Name 1', key: '0-0', children: [{ title: 'Name 1-1', key: '0-0-0', id: 4, children: [{ title: 'Name 1-3', key: '0-0-0-0', id: 3, visibility: false, children: [{ title: 'Name 1-4', key: '0-0-0-0', id: 34, visibility: false }] }, { title: 'Name 2-1', key: '0-0-1', id: 1, visibility: false } ] }] }]
const findById = (data, id) => {
var found, s = (d, id) => d.find(x => x.id == id ? found = x : s(x.children, id))
s(data, id)
return found ? found : false
}
let el = findById(data, 34)
el.visibility = true
console.log(el)
console.log(data)
This will also find the element and return it so you can change anything you want on it.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can do something like this via a recursive function:
let data = [{ title: 'Name 1', key: '0-0', children: [{ title: 'Name 1-1', key: '0-0-0', id: 4, children: [{ title: 'Name 1-3', key: '0-0-0-0', id: 3, visibility: false, children: [{ title: 'Name 1-4', key: '0-0-0-0', id: 34, visibility: false }] }, { title: 'Name 2-1', key: '0-0-1', id: 1, visibility: false } ] }] }]
const findById = (data, id) => {
var found, s = (d, id) => d.find(x => x.id == id ? found = x : s(x.children, id))
s(data, id)
return found ? found : false
}
let el = findById(data, 34)
el.visibility = true
console.log(el)
console.log(data)
This will also find the element and return it so you can change anything you want on it.
add a comment |
up vote
1
down vote
You can do something like this via a recursive function:
let data = [{ title: 'Name 1', key: '0-0', children: [{ title: 'Name 1-1', key: '0-0-0', id: 4, children: [{ title: 'Name 1-3', key: '0-0-0-0', id: 3, visibility: false, children: [{ title: 'Name 1-4', key: '0-0-0-0', id: 34, visibility: false }] }, { title: 'Name 2-1', key: '0-0-1', id: 1, visibility: false } ] }] }]
const findById = (data, id) => {
var found, s = (d, id) => d.find(x => x.id == id ? found = x : s(x.children, id))
s(data, id)
return found ? found : false
}
let el = findById(data, 34)
el.visibility = true
console.log(el)
console.log(data)
This will also find the element and return it so you can change anything you want on it.
add a comment |
up vote
1
down vote
up vote
1
down vote
You can do something like this via a recursive function:
let data = [{ title: 'Name 1', key: '0-0', children: [{ title: 'Name 1-1', key: '0-0-0', id: 4, children: [{ title: 'Name 1-3', key: '0-0-0-0', id: 3, visibility: false, children: [{ title: 'Name 1-4', key: '0-0-0-0', id: 34, visibility: false }] }, { title: 'Name 2-1', key: '0-0-1', id: 1, visibility: false } ] }] }]
const findById = (data, id) => {
var found, s = (d, id) => d.find(x => x.id == id ? found = x : s(x.children, id))
s(data, id)
return found ? found : false
}
let el = findById(data, 34)
el.visibility = true
console.log(el)
console.log(data)
This will also find the element and return it so you can change anything you want on it.
You can do something like this via a recursive function:
let data = [{ title: 'Name 1', key: '0-0', children: [{ title: 'Name 1-1', key: '0-0-0', id: 4, children: [{ title: 'Name 1-3', key: '0-0-0-0', id: 3, visibility: false, children: [{ title: 'Name 1-4', key: '0-0-0-0', id: 34, visibility: false }] }, { title: 'Name 2-1', key: '0-0-1', id: 1, visibility: false } ] }] }]
const findById = (data, id) => {
var found, s = (d, id) => d.find(x => x.id == id ? found = x : s(x.children, id))
s(data, id)
return found ? found : false
}
let el = findById(data, 34)
el.visibility = true
console.log(el)
console.log(data)
This will also find the element and return it so you can change anything you want on it.
let data = [{ title: 'Name 1', key: '0-0', children: [{ title: 'Name 1-1', key: '0-0-0', id: 4, children: [{ title: 'Name 1-3', key: '0-0-0-0', id: 3, visibility: false, children: [{ title: 'Name 1-4', key: '0-0-0-0', id: 34, visibility: false }] }, { title: 'Name 2-1', key: '0-0-1', id: 1, visibility: false } ] }] }]
const findById = (data, id) => {
var found, s = (d, id) => d.find(x => x.id == id ? found = x : s(x.children, id))
s(data, id)
return found ? found : false
}
let el = findById(data, 34)
el.visibility = true
console.log(el)
console.log(data)
let data = [{ title: 'Name 1', key: '0-0', children: [{ title: 'Name 1-1', key: '0-0-0', id: 4, children: [{ title: 'Name 1-3', key: '0-0-0-0', id: 3, visibility: false, children: [{ title: 'Name 1-4', key: '0-0-0-0', id: 34, visibility: false }] }, { title: 'Name 2-1', key: '0-0-1', id: 1, visibility: false } ] }] }]
const findById = (data, id) => {
var found, s = (d, id) => d.find(x => x.id == id ? found = x : s(x.children, id))
s(data, id)
return found ? found : false
}
let el = findById(data, 34)
el.visibility = true
console.log(el)
console.log(data)
answered yesterday
Akrion
7,07611222
7,07611222
add a comment |
add a comment |
do you want to updatte or to get a whole new data structure with the updated item? btw, what have you tried?
– Nina Scholz
yesterday
you need something like map and then a rekursive function over all childreans
– Tobias K
yesterday
Did you look around? This question must have been asked many times before.
– trincot
yesterday