简体   繁体   中英

Delete values from from 1 string/array based on another string/array Javascript

I have two strings/arrays. I know, I find it hard to tell the difference. It's too early to digress though.

String one is a comma separated string for arguments sake. So is string two. String two contains some, but not all of the values found in string one.

What I'm trying to do is subtract the values from string one that are found in string two.

There are a whole load of variations of this question and answers both on SA and the rest of googledom. However nothing has worked for me. Every time errors were thrown up.

The code below is the closest I have managed to get, but for some reason, in the console, only the first part of string one is counted, not the rest that proceed it. This may be where the string/array mixup comes in. The example given works perfectly on SA Link to the example

The code used is as follows:

var defendantList = 5545,
    goo,
    holly1;
var dismissedDefendants = holly1;
for (var i in defendantList) {
  for (var j in dismissedDefendants) {
    if (defendantList[i].value === dismissedDefendants[j].value) {
      var index = defendantList.indexOf(defendantList[i]);
      if (index > -1) {
          defendantList.splice(index, 1);
      }
    }
  }
}
console.log(defendantList);

The console log just returns 5545.

So is this possible to do when both (strings?) are comma separated?

You're declaring separate variables, not a list of variables, here:

var x = 1, y;

That declares x and sets it to 1, and declares y and doesn't set it to anything (so y === undefined ).

To create a list that can be used with for in (or probably better to use for of if you can use modern JS), use [] to create an array:

var defendants = [5545, foo, etc];

If you apply this modification to the above code directly, you will get a ReferenceError because foo and etc aren't declared anywhere ( goo and holly1 in the example). It "works" in the example because you are declaring them with the var... syntax, which takes a comma-separated list of variables to declare but does not create a list containing those variables.

Also note that .value is specific to the example you referenced -- the array elements are defined as objects with properties like value: "Joe Blow" etc. If you just want to compare strings, not objects, you would do something like this:

var defendants = ["5545", "goo", "holly1"];

Then change the if to simply if (defendants[i] === dismissedDefendants[j]) .

Your task can be done in a much simpler way, see below:

 var defendants = ["5545", "goo", "holly1", "cat", "dog"], dismissedDefendants = ["holly1", "holly2", "dog"]; // remove all elements of dismissedDefendants from defendants var leftOverDefendants=defendants.filter(d=>dismissedDefendants.indexOf(d)<0); // this behaves nicely, even if elements of dismissedDefendants don't exist in defendants console.log(leftOverDefendants);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM