Chaining multiple array methods in javascript -


i trying chain multiple methods in array.

original array:

this.array = [1, 2, 3, 4, 5, 6]; 

clone:

this.array.slice(0); 

change first element

this.array.splice(0, 0, 10); 

running these individually works.

combining both:

this.array.slice(0).splice(0, 0, 10); 

this doesnot work. why?

because this.array.splice(0, 0, 10); return new array(containing removed elements - in case empty array since no element removed) not source array on called on.

in case using clone of original array, loosing reference cloned instance.

so this.array.slice(0) return clone on .splice(0, 0, 10) performed(it update cloned object) splice operation return new array(with removed objects) not cloned instance looses reference it

so solution use temp reference like

var tmp = this.array.slice(0); tmp.splice(0, 0, 10) 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -