Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript add and remove multiple value from a list in and from specific index

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
like image 432
gamer Avatar asked Jun 21 '16 05:06

gamer


2 Answers

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

like image 170
RIYAJ KHAN Avatar answered Oct 21 '22 11:10

RIYAJ KHAN


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:

  1. I first workout what needs to be extracted from the array (data).
  2. Similarly for the element that needs to be put after.
  3. I loop through the initial array to find that element mentioned in point (2), then start putting whatever needs to come after behind it.
  4. Now you will have an array which consists of the original value plus the new items.
  5. Using the garbage collection concept, I go through the new array and mark the "to-be-deleted" element as null. This is so that I know which element to delete with splice later.
  6. Once again, I loop through the new array looking for the flagged element (null) and remove them.

Now you will left with what you wanted.

Output:

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

indexOf 'ball' = 4

indexOf 'dog' = 5

like image 1
Samuel Toh Avatar answered Oct 21 '22 12:10

Samuel Toh