Javascript add and remove multiple values ​​from a list in and from a specific index

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 
+5
source share
4 answers

you can do it 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

+1
source

A difficult problem, but it was fun to solve it.

This is my decision, perhaps not the most effective, but, unfortunately, he did the job.

 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 decomposed your problem into several small ones and systematized them.

My approach is the first loop through an array of data and push of all elements, including new ones. Then go to the list and mark those elements that are in position, the variable index array "as null" and delete them.

Passing Algorithm:

  • First I train what needs to be extracted from the array (data).
  • Similarly for the item to be placed after.
  • I go through the initial array to find this element mentioned in (2), then start investing everything you need after it.
  • You will now have an array that consists of the original value plus new elements.
  • Using the concept of garbage collection, I look through a new array and mark the "to-be-deleted" element as null. This is so that I know which element to remove with the splice later.
  • Once again, I go through a new array looking for the marked (null) and delete them.

Now you are left with what you want.

Output:

['apple', 'cat', 'elephant', 'fish', 'ball', 'dog', 'gorilla']

indexOf 'ball' = 4

indexOf 'dog' = 5

+1
source

Now it works in place and uses some bias indicators, such as left for the retrieving value and right for inserting the value.

After completion, the indices are calculated based on the left offset.

 Example for from = [1, 3, 6], to = 5 from from to from offset 0 1 2 3 4 5 6 7 left right -------- -------- -------- -------- -------- -------- -------- -------- ----- ----- apple [ball] cat [dog] elephant (fish) gorilla [banana] 0 0 apple cat [dog] elephant (fish) [ball] gorilla [banana] -1 0 apple cat elephant (fish) [ball] [dog] gorilla [banana] -2 0 apple cat elephant (fish) [ball] [dog] [banana] gorilla -2 1 

 function juggle(data, from, to) { var left = 0, right = 0; from.forEach(function (a) { if (a + left < to) { data.splice(to, 0, data.splice(a + left, 1)[0]); left--; } else { right++; data.splice(to + right, 0, data.splice(a, 1)[0]); } }); from.forEach(function (_, i, a) { a[i] = to + left + i + 1; }); } var data = ["apple", "ball", "cat", "dog", "elephant", "fish", "gorilla", "banana"], index = [1, 3, 7], // ball dog banana to = 5; // fish juggle(data, index, to); console.log(data); console.log(index); data = ["apple", "ball", "cat", "dog", "elephant", "fish", "gorilla", "banana"]; index = [4, 5]; // elephant fish to = 2; // cat juggle(data, index, to); console.log(data); console.log(index); 
+1
source

Wow, nice problem, really enjoyed hacking my way through this. I used a functional approach since it has not been done yet:

 var data = ["apple", "ball", "cat", "dog", "elephant", "fish", "gorilla"] var index = [2, 3] // ball and dog var to = 5 // fish var moveItemsToPosition = function (data, indices, to) { var movingItems = []; var after = data[to + 1]; for (var i = 0 ; i < indices.length; i++) { // takes out the items that will be moved form the original array movingItems.push(data[indices[i] - i]); data.splice(indices[i] - i, 1); } // finds the new position of the item to move next to var pos = data.indexOf(after); var data2 = data.slice(pos, data.length); // join the items that will be moved data = data.slice(0, pos).concat(movingItems); data = data.concat(data2); // update the indices for (var i = 0 ; i < indices.length; i++) { // get the new indices indices[i] = data.indexOf(movingItems[i]); } return [data, indices]; } console.log(moveItemsToPosition(data, index, to)); 

It returns both an updated array and new indexes. You can remove this if necessary. Basically, the algorithm first extracts the elements that need to be moved, and then finds the element next to which it should be moved, and places it there (using fancy JS).

Hope this helps!

+1
source

All Articles