alloy
alloy

Reputation: 766

Splicing Array Based on For Loop Iteration

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?

Upvotes: 0

Views: 92

Answers (1)

bloodyKnuckles
bloodyKnuckles

Reputation: 12079

"The splice() method adds/removes items to/from an array, and returns the removed item(s)." http://w3schools.com/jsref/jsref_splice.asp So, basically, you're reducing workshops to a single object, the removed array element.

Instead, change:

workshops = workshops.splice(j, 1);
selectedWorkshops = selectedWorkshops.splice(i, 1);

...to:

workshops.splice(j, 1);
selectedWorkshops.splice(i, 1);

JSFiddle


Maybe this is what you want:

var registeredWorkshops = [];
var flipWorkshops = function(){
    var numberOfWorkshops = selectedWorkshops.length;
    var X = 1;
    for(var i = numberOfWorkshops - 1; i >= 0; i-- ){
        registeredWorkshops.push(selectedWorkshops[i]);
        var selectedWorkshop = selectedWorkshops[i];
        for(var j = workshops.length - 1; j >= 0; j--) {
            var originalWorkshop = workshops[j];
            if(selectedWorkshop.name == originalWorkshop.name &&
              selectedWorkshop.WorkshopId == originalWorkshop.WorkshopId){
                var matchingWorkshop = j;
                workshops.splice(j, 1);
                selectedWorkshops.splice(i, 1);
            }
        }
    }
};
flipWorkshops();

Fixed the splice, reversed the loop, as suggested by jwatts1980, moved the selectedWorkshop var out of the loop, and compared each object item, rather than comparing objects, since objects don't compare.

JSFiddle Tweaked

JSFiddle Object Compare Example

Upvotes: 2

Related Questions