0

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?

1
  • 1
    "The splice() method adds/removes items to/from an array, and returns the removed item(s)." w3schools.com/jsref/jsref_splice.asp So, basically, you're reducing workshops to a single object, the removed array element. Commented Apr 14, 2015 at 22:40

1 Answer 1

2

"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

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, I think that since items are being removed from the array the for loop also needs to be traversed in reverse. for(var j = workshops.length - 1; j >= 0; j--)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.