I have code like:
var data = ["apple", "ball", "cat", "dog", "elephant", "fish", "gorilla"] var index = [1, 3] // ball and dog var to = 5 // fish for(var i in index){ console.log(index[i]) var local_data = data data.splice(to, 0, data.splice(index[i]), 1) } console.log(data) console.log(index)
Jsfiddle
Here var index = [1,3] is the index value for the data to be set.
I want here to set the value of index i. e ball and dog after fish , and the rest remains in order.
After insertion, I want the index value to change according to the new position of ball and dog i. e [4, 5]
Updata STRONG>
In the end I want to get the result: console.log(data) should give
["apple", "cat", "elephant", "fish", "ball", "dog", "gorilla"]
and console.log(index) should indicate:
[4, 5] // new value of ball and dog
source share