1

I am using angularjs and I have this line of code:

// Get our elements
var elements = angular.element(options.animation.element).children().children();

with this collection I would like to take a certain amount of elements and move them from the end to the front. I tried like this:

var stop = elements.length - options.itemsToShow,
    elementsToMove = elements.splice(stop, options.itemsToShow);

elements.unshift(elementsToMove);

but I get an error stating

TypeError: elements.unshift is not a function

Does anyone know how I can fix this?

r3plica
  • 13,017
  • 23
  • 128
  • 290

1 Answers1

2

elements is not a array to apply array methods. Use Array.from over array-like collection.

The Array.from() method creates a new Array instance from an array-like or iterable object.

Array.from(elements).unshift(elementsToMove);

Note: Also consider using Polyfill due to lack of Browser compatibility

Or [].slice.call could be used as well!

Community
  • 1
  • 1
Rayon
  • 36,219
  • 4
  • 49
  • 76
  • using angular. will any of these actually update the DOM? Creating a new array won't work because angular works with 2 way binding, so i need to actually modify the collection itself – r3plica Jul 31 '16 at 16:06
  • I am unable to relate things with `2-way binding` with `new array`.. Have you tried this solution ? – Rayon Jul 31 '16 at 16:08