0

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?

2
  • loops don't work that way developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Dec 6, 2020 at 17:40
  • Ah ok thanks for that @Mister Jojo. So the plethora of other answers out there for similar questions always throw up errors, mainly along the lines of xx isn't a function. Any ideas how I can achieve the desired result? Commented Dec 6, 2020 at 18:18

2 Answers 2

0

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);

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

1 Comment

Thank you @cars10m your answer is very straight forward and does the trick. I'm 3 points off being able to publically mark it correct but I will come back to do so once i've a good enough rep. It turns out many solutions that I have researched would have also done the trick. The main problem lies in how the shortcode is displaying the string. But that's not a problem for SA. Thanks again
0

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]).

1 Comment

Ah ok thank you @Gus. I'm displaying the values via a Wordpress shortcode and I was sure doing [[shortcode]] removed the outside square brackets. I'm replying on my phone at the minute so I will try and implement your suggestion tomorrow. Many thanks

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.