Answer the question
In order to leave comments, you need to log in
How to write to a multidimensional array/object at an address in JavaScript?
Greetings. Here I do not understand how to write data to a multidimensional array with objects at the address. For example, there is an array, an address (by the key property) and new data
var data = [
{key:'raz', cells:[{val: '1'},{val:'razoviy'}]},
{key:'dva', cells:[{val: '2'},{val:'vtoroy'}],children:[
{key:'poddva', cells:[{val: '2'},{val:'podvtoroy'}],children:
[{key:'podpoddva', cells:[{val: '2'},{val:'podpodvtoroy'}]},{key:'raz', cells:[{val: '1'},{val:'raz'}]}]
},
{key:'poddvadva', cells:[{val: '2'},{val:'podvtoroyvtoroy'}]}
]},
{key:'tri', cells:[{val: '3'},{val:'trtiy'}]}
]
var adres = ['dva','poddva','raz']
var newDataCells = [{val: '1.0'},{val:'raz raz, est svyaz'}]
Answer the question
In order to leave comments, you need to log in
Solved the problem like this:
var data = [
{key:'raz', cells:[{val: '1'},{val:'razoviy'}]},
{key:'dva', cells:[{val: '2'},{val:'vtoroy'}],children:[
{key:'poddva', cells:[{val: '2'},{val:'podvtoroy'}],children:
[{key:'podpoddva', cells:[{val: '2'},{val:'podpodvtoroy'}]},{key:'raz', cells:[{val: '1'},{val:'raz'}]}]
},
{key:'poddvadva', cells:[{val: '2'},{val:'podvtoroyvtoroy'}]}
]},
{key:'tri', cells:[{val: '3'},{val:'trtiy'}]}
]
var adres = ['dva','poddva','raz']
var newDataCells = [{val: '1.0'},{val:'raz raz, est svyaz'}]
var lastMath
Object.traverse(data, function(node, value, key, path, depth) {
if(typeof value == 'object' && value.key){
let indexAdres = 0;
if(depth > 0)
indexAdres = depth/2
if(value.key == adres[indexAdres])
lastMath = value
}
});
lastMath.cells = newDataCells
console.log(data)
var data = [
{key:'raz', cells:[{val: '1'},{val:'razoviy'}]},
{key:'dva', cells:[{val: '2'},{val:'vtoroy'}],children:[
{key:'poddva', cells:[{val: '2'},{val:'podvtoroy'}],children:
[{key:'podpoddva', cells:[{val: '2'},{val:'podpodvtoroy'}]},{key:'raz', cells:[{val: '1'},{val:'raz'}]}]
},
{key:'poddvadva', cells:[{val: '2'},{val:'podvtoroyvtoroy'}]}
]},
{key:'tri', cells:[{val: '3'},{val:'trtiy'}]}
]
var newDataCells = [{val: '1.0'},{val:'raz raz, est svyaz'}]
var obj = data.find(val => val.key == 'dva')
obj.cells = newDataCells;
console.log(data);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question