I would like to be able to order an array by a nested object.
I have this array containing informations about objects on a timeline I would be able to order this by the start position that is defined inside nested arrays. I can get it iterate trough the array with lines[0].events[0].start
this is the array:
timelines = [
{
name: 'obj1',
data: { id : 'obj1-guid' },
lines: [{
events: [{
name: 'animation1',
data : { id : 'animation1-guid' },
start : 100,
duration : 200
}]
}],
video_url: 'url',
},
{
name: 'obj2',
data: { id : 'obj2-guid' },
lines: [{
events: [{
name: 'animation1',
data : { id : 'animation1-guid' },
start : 4,
duration : 200
}]
}],
video_url: 'url',
},
{
name: 'obj3',
data: { id : 'obj3-guid' },
lines: [{
events: [{
name: 'animation1',
data : { id : 'animation1-guid' },
start : 56,
duration : 200
}]
}],
video_url: 'url',
},
];
I tried a function like this
function sorting(json_object, key_to_sort_by) {
function sortByKey(a, b) {
var x = a[key_to_sort_by];
var y = b[key_to_sort_by];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
json_object.sort(sortByKey);
}
timelines = sorting(timelines, 'lines[0].events[0].start');
but of course it's not working
sortingfunction in the first place...'lines[0].events[0].start'is not a valid key.lines[].events[].start??