2

I have array of strings

What i want to do is i want to find matching element of dataArr in oneArr and twoArr and want to remove it.

Here is my code i have tried but its not working:

dataArr = ["1","3","9"];
oneArr  = ["1","2","5"];
twoArr  = ["4","9"];

 updateData(dataArr){
 for (let index = 0; index < dataArr.length; index++) {

            let orgIndx = oneArr.findIndex(dataArr[index]);
            oneArr.splice(orgIndx,1);

            let orgIndx1 = twoArr.findIndex(dataArr[index]);
            twoArr.splice(orgIndx1,1);

}
console.log("oneArr = "+oneArr);
console.log("twoArr = "+twoArr);

Where i am doing mistke please help

1
  • 1
    Why [android] tag Commented Sep 26, 2018 at 9:36

4 Answers 4

5

Use filter as below

var dataArr = ["1","3","9"];
var oneArr  = ["1","2","5"];
var twoArr  = ["4","9"];

oneArr = oneArr.filter( e => dataArr.indexOf(e) == -1);
twoArr = twoArr.filter( e => dataArr.indexOf(e) == -1);

console.log("oneArr = "+oneArr);
console.log("twoArr = "+twoArr);

Sign up to request clarification or add additional context in comments.

2 Comments

What is indexOf(e) in this?
indexOf(e) is the index in dataArr of e. i,e: the position of e in dataArr. Have a look here - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
1

The splice method does not affect the current array, it returns a new one.

For example (taken from the MDN):

var myFish = ['angel', 'clown', 'drum', 'mandarin', 'sturgeon'];
var removed = myFish.splice(3, 1);

// removed is ["mandarin"]
// myFish is ["angel", "clown", "drum", "sturgeon"]

You will need to do something like such:

updateData(dataArr){
  const newArrOne = [];
  const newArrTwo = []
  for (let index = 0; index < dataArr.length; index++) {
    let orgIndx = oneArr.findIndex(dataArr[index]);
    newArray = oneArr.splice(orgIndx,1);

    let orgIndx1 = twoArr.findIndex(dataArr[index]);
    newArrTwo = twoArr.splice(orgIndx1,1);
  }

  console.log(newArrOne);
  console.log(newArrTwo);
}

Please note that the above code is nowhere near production code, is just based on the code you provided.

You could also use the filter method as explained by Harunur Rashid

Comments

1
for (var index = 0; index < dataArr.length; index++) {
  let orgIndx = oneArr.indexOf(dataArr[index]);
  if (orgIndx >= 0) oneArr.splice(orgIndx, 1);

  let orgIndx1 = twoArr.indexOf(dataArr[index]);
  if (orgIndx1 >= 0) twoArr.splice(orgIndx1, 1);
}

Comments

0

The mistake you are making is, you are passing a string to the findIndex function as parameter but it expects a function as parameter. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

If you want to fix your current code i.e. by using find index, you can do it like the following:

updateData(dataArr){
     for (let index = 0; index < dataArr.length; index++) {

                let orgIndx = oneArr.findIndex((element)=>{
                     return element==dataArr[index]
                });

                if(orgIndx!=-1)
                oneArr.splice(orgIndx,1);

                let orgIndx1 = twoArr.findIndex((element)=>{
                    return element==dataArr[index]
                });
                if(orgIndx1!=-1)
                twoArr.splice(orgIndx1,1);

    }
}

In the example above, i have modified your logic by providing a function as parameter to findIndex function and also, checking if index=-1 before splicing because findIndex returns -1 if no element found in the array satisfying the condition of the function passed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.