I am trying to take an array ('selectedWorkshops'), and move objects in it to 'registeredWorkshops'. Then, I also want to remove those objects from both 'selectedWorkshops' and another array simply called 'workshops'.
See my codepen here: http://codepen.io/trueScript/pen/wBVqNN
Arrays:
var workshops = [
{
name: 'apples',
WorkshopId: '19'
},
{
name: 'oranges',
WorkshopId: '3b'
},
{
name: 'pears',
WorkshopId: 'x6'
},
{
name: 'pineapples',
WorkshopId: '55'
},
{
name: 'watermelons',
WorkshopId: '8v'
}
];
var selectedWorkshops = [
{
name: 'oranges',
WorkshopId: '3b'
},
{
name: 'watermelons',
WorkshopId: '8v'
},
{
name: 'pears',
WorkshopId: 'x6'
}
];
var registeredWorkshops = [];
Function that is supposed to move workshops to 'registeredWorkshops' and remove them from 'selectedWorkshops' and 'workshops':
flipWorkshops = function(){
var numberOfWorkshops = selectedWorkshops.length;
var X = 1;
for(var i = 0; i < numberOfWorkshops; i++ ){
registeredWorkshops.push(selectedWorkshops[i]);
for(var j = 0, arrayLength = workshops.length; j < arrayLength; j++) {
var selectedWorkshop = selectedWorkshops[i];
var originalWorkshop = workshops[j];
if(selectedWorkshop == originalWorkshop){
var matchingWorkshop = j;
workshops = workshops.splice(j, 1);
selectedWorkshops = selectedWorkshops.splice(i, 1);
}
}
}
};
flipWorkshops();
Why aren't the objects being properly spliced out of the 'workshop' and 'selectedWorkshops' arrays like they should be? What am I doing wrong?
workshopsto a single object, the removed array element.