0

I have 2 array and want to remove the elements of 2nd array as per position numbers on 1st array.

var notexists = []; //value is "1","5","8" on 0,1,2nd position. 
var copyrequiredfields = [];//Value is "a","b","c","d","e","f",...
for (var i = 0; i < notexists.length; i++) {
     copyrequiredfields.splice(parseInt(notexists[i]), 1);
}

as per example i want to remove 1st 5th and 8th element from copyrequiredfields . Please suggest some answer.

5
  • There are probably a lot of ways to do this. Could you give more detail on what the overall goal is you are trying to achieve? It would be easier to suggest a solution if we knew why you wanted to do this. Commented Oct 8, 2016 at 5:35
  • 1
    Use slice if you're basing the results on position. Of course, slice does not affect the initial array, like splice does. Note that splice takes a count, not a position. Commented Oct 8, 2016 at 5:35
  • So by 1st element you mean a? Commented Oct 8, 2016 at 5:41
  • @Rajesh Yes I mean a by 1st element Commented Oct 8, 2016 at 5:44
  • @chhaya_patel I have added answer with both array.splice and array.filter. Hope it helps! Commented Oct 8, 2016 at 5:54

3 Answers 3

1

Create a new array, iterate the copyrequiredfields, when the index in notexists, ignore it.

example:

var notexist = [1,2,5];
var copyrequiredfields = ['a','b','c','d','e','f','g'];
//create a index map
var index_map = {};
for(var i = 0; i < notexist.length; i++){
    index_map[notexist[i]] = true;
}
//remove the elements
var newarray = [];
for(var i = 0; i < copyrequiredfields.length; i++){
    if(!index_map[i]){
        newarray.push(copyrequiredfields[i]);
    }
}
copyrequiredfields = newarray;
Sign up to request clarification or add additional context in comments.

Comments

1

In JS, index of array starts with 0 and not 1 so you just have to subtract 1 from value before splice. Rest of the code is fine.

One issue is, as you remove elements from array, elements after it are moved 1 position back. This will give you incorrect output. 1 hack is to count number of elements removed to count movement.

A better solution would be to use array.filter.

Array.splice

var notexists = ["1", "5", "8"]; //value is  on 0,1,2nd position. 
var copyrequiredfields = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]; //Value is "a","b","c","d","e","f",...
var count=0;
for (var i = 0; i < notexists.length; i++) {
  console.log(+notexists[i] - 1)
  copyrequiredfields.splice(+notexists[i]-1-count++, 1);
}

console.log(copyrequiredfields)

Array.filter

var notexists = ["1", "5", "8"]; //value is  on 0,1,2nd position. 
var copyrequiredfields = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]; //Value is "a","b","c","d","e","f",...
var r = copyrequiredfields.filter((x,i)=>notexists.indexOf((i+1).toString()) === -1)
console.log(r)

Comments

0

You could copy the whole array, except of the parts you dont need anymore:

var tempCopy = [];
for (var i=0; i < copyrequiredfields.length; i++)
    if(notexists.indexOf(i)==-1)
        tempCopy.push(copyrequiredfields[i]);
copyrequiredfields = tempCopy;

Greets!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.