I have a 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 of the data to be set.
What I want here is, to set the value of index
i. e ball
and dog
after fish
and the rest remains on the order.
After inserted I want the index
value to be changed according to the new position of the ball
and dog
i. e [4, 5]
Updata
In the end I want the result like:
console.log(data)
should give
["apple", "cat", "elephant", "fish", "ball", "dog", "gorilla"]
and console.log(index)
should give:
[4, 5] // new value of ball and dog
you can do it in this way.
var data = ["apple", "ball", "cat", "dog", "elephant", "fish", "gorilla"]
var index = [4, 5];
var to = 2 ;
var arrTemp = [];
data.forEach(function(val,key){
if(index.indexOf(key)!=-1){
data.splice((to+1),0,data[key]);
to++;
delete data[key];
}
})
data = data.filter(function(val){ return val != undefined });
console.log(data)
UPDATE :
var data = ["apple", "ball", "cat", "dog", "elephant", "fish", "gorilla"]
var index = [ 2,3];
var to = 5 ;
var arrTemp = [];
data.forEach(function(val,key){
if(index.indexOf(key)!=-1){
arrTemp.push(data[key]);
delete data[key];
}
})
to=to+1;
for(var pos=0;pos<index.length;pos++){
data.splice(to++,0,arrTemp[pos])
}
data = data.filter(function(val){ return val != undefined });
JsFiddle
Tricky problem but was fun solving it.
This is my solution, probably not the most efficient one but hey it got the work done.
var data = ["apple", "ball", "cat", "dog", "elephant", "fish", "gorilla"];
var output = [];
var index = [1, 3]; // ball and dog
var to = 5; // fish
var extractValues = [];
index.forEach(function(i){
extractValues.push(data[i]);
});
var valueToPutAfter = data[to];
// Put the value first
data.forEach(function(element) {
if (element === valueToPutAfter) { // found element to push after
output.push(element);
extractValues.forEach(function(value){
output.push(value);
});
}
else {
output.push(element);
}
});
// Mark the position that needs to be deleted as null
var prevDelIndex = 0;
index.forEach(function(i){
output[i] = null;
});
// delete the elements
output.forEach(function(element, index) {
if (element == null) {
output.splice(index, 1);
}
});
console.log(output);
console.log(output.indexOf("ball"));
console.log(output.indexOf("dog"));
I have decompose your problem into a few small ones and tackle them in a systematic manner.
My approach is first loop through the data array and push all the elements including the new ones. Then go through the list again and mark those elements found in position, variable index array" as null and remove them.
Algorithm walkthrough:
Now you will left with what you wanted.
Output:
[ 'apple', 'cat', 'elephant', 'fish', 'ball', 'dog', 'gorilla' ]
indexOf 'ball' = 4
indexOf 'dog' = 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With