Update a value in a nested array while maintaining original indexes ES6 [duplicate]

Multi tool use
Multi tool use











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.










share|improve this question















marked as duplicate by trincot javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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















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.










share|improve this question















marked as duplicate by trincot javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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













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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Josh

50036




50036










asked yesterday









GoranR

194




194




marked as duplicate by trincot javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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


















  • 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












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.






share|improve this answer




























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        Akrion

        7,07611222




        7,07611222















            yy,3bs,5svRQ KJ,eIfo4aE SgHkUw,x
            nJoJ6,pdWVwqioEVKPCnR7,ZKkmg8OnjJ1fFrRmZBXv j6z X Ty6ewmrCfn,3x,CVcj bhzC,0zL

            Popular posts from this blog

            UPSERT syntax error linked to UPDATE in PostgreSQL (python)

            Some classess of my CSS file are not rendering into Django templates (most classess render without problems)

            Sphinx de Gizeh