I'll cut to the chase.
I want to change the radius value in the following array for a specific ID and dataID:
var data = [{
ID: 0,
type: 'circle',
Name: 'Tom',
MoreData: [{
dataID: 123,
dimData: {
radius: 25,
var: 15
}
}, {
dataID: 345,
dimData: {
radius: 35,
var: 65
}
}]
}, {
ID: 1,
type: 'circle',
Name: 'Mat',
MoreData: [{
dataID: 678,
dimData: {
radius: 40,
var: 30
}
}, {
dataID: 91011,
dimData: {
radius: 50,
var: 50
}
}]
}];
My current solution:
var data = [{
ID: 0,
type: 'circle',
Name: 'Tom',
MoreData: [{
dataID: 123,
dimData: {
radius: 25,
var: 15
}
}, {
dataID: 345,
dimData: {
radius: 35,
var: 65
}
}]
}, {
ID: 1,
type: 'circle',
Name: 'Mat',
MoreData: [{
dataID: 678,
dimData: {
radius: 40,
var: 30
}
}, {
dataID: 91011,
dimData: {
radius: 50,
var: 50
}
}]
}];
var doSomething = function(array, IDVal, dataIDVal, RadiusVal) {
//Search array for ID and then dataID and then set the value of that dataID's Radius to RadiusVal
for (var i = 0; i < array.length; i++) {
if (array[i].ID == IDVal) {
for (var j = 0; j < array[i].MoreData.length; j++) {
if (array[i].MoreData[j].dataID == dataIDVal) {
console.log('Radius Before: ' + array[i].MoreData[j].dimData.radius);
array[i].MoreData[j].dimData.radius = 20000;
console.log('Radius After: ' + array[i].MoreData[j].dimData.radius);
}
}
}
}
}
doSomething(data, 0, 345, 100);
console.log(data[0]);
I want to first search for the correct ID, then get the correct dataID to change the dimData.Radius.
Is there a more efficient way to do this? Needless to say, the data array can be very large and the MoreData array also contains more values (as does dimData). I was wondering if there is a faster way to achieve the above result.
EDIT: To answer a few questions, the only thing guaranteed is that the ID value will always be structured so that it increments by 1 everytime, but I don't believe the data is sorted otherwise. However, I would appreciate an answer assuming the dataset is sorted too.
EDIT 2: Attempted a solution using .filter, but ended up with several functions nested inside each other which just looked confusing.
If anyone wants that soltuion, its here: How do I pass an extra parameter to the callback function in Javascript .filter() method?
Link on JS Fiddle if Code Snippet doesn't work (I'm new): https://jsfiddle.net/53pa2xst/