0

Given this:

var results = [
  {
        "Title": "Battle of Baekgang",
        "Space": ["South Korea"]
    },
    {
        "Title": "Victory Tests",
        "Space": ["United Kingdom"]
    },
    {
        "Title": "Everett massacre",
        "Space": ["United States"]
    },
    {
        "Title": "Bologna massacre",
        "Space": ["Italy"]
    },
    {
        "Title": "Milano massacre",
        "Space": ["Italy"]
    }
];  

How would I loop it in order to say:

If Space value matching myNation {do this}?

Having set var countries = ["Italy", United Kingdom", "South Korea"];

I have var regex = new RegExp(countries.join("|"), "i");

And I know i could do if(location.match(regex)) {

But I need to first store location I guess as the name of Space value

Or any other/better way?

UPDATE

I have a map with polygons and each polygons have classes like:

<path class="italy france germany">

I could have Nclasses names for each country, the json I get would have one object called Space with one or more countries in it, so I'd need to check if any of the json countries, matches any class in any polygons paths on my map and if so, the path should get a class active added to it.

8
  • Do you mean if the "Space" value is matching one of the countries in myNation? Commented Aug 5, 2017 at 21:30
  • @abagshaw yes, just updated the question to make it clearer Commented Aug 5, 2017 at 21:31
  • @abagshaw updated again Commented Aug 5, 2017 at 21:33
  • 1
    codepen.io/DZuz14/pen/QMdNNe?editors=0012 Any help? Commented Aug 5, 2017 at 21:34
  • 1
    BTW, there's no JSON in your question. It might have been once before you parsed it, but that's just an array of objects assigned to a variable. Commented Aug 5, 2017 at 21:49

4 Answers 4

2

If I understand your question properly, this should work:

var results = [{
        "Title": "Battle of Baekgang",
        "Space": ["South Korea"]
    }, {
        "Title": "Victory Tests",
        "Space": ["United Kingdom"]
    }, {
        "Title": "Everett massacre",
        "Space": ["United States"]
    }, {
        "Title": "Bologna massacre",
        "Space": ["Italy"]
    }, {
        "Title": "Milano massacre",
        "Space": ["Italy"]
    }
];

var countries = ["Italy", "United Kingdom", "South Korea"];
for (var i = 0; i < results.length; i++) {
    for (var j = 0; j < results[i]["Space"].length; j++) {
        if(countries.includes(results[i]["Space"][j])) {
            var matchedCountry = results[i]["Space"][j];
            var currentTitle = results[i]["Title"];
            //Do other stuff
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

what if we had a path on a map with a class matching the Space value, once it is true and we get to Do this, which would be the matched nation?
Sorry, not sure what you mean. "Path on a map"?
i have a map with polygons and each polygon is a path, with a class as nation names, so if we match a country within the json, I should apply an active class to the path
I think you are asking two separate questions here. Find a solution here that answers this question and then maybe post another question separately for the second part (unless I'm misunderstanding this and the two problems are linked)
they are, in your answer which i like, how would I know what country names has been matched in roder to then be able to compare it with a class element i might have?
|
2
 results.filter(
   ({Space})=>countries.includes(Space[0])
 ).forEach(el=>console.log(el))

Simply filter, then loop over the filtered :/


Update: you may create just one Space Set:

var spaces = new Set(
  results.reduce((acc,el)=>[...acc,...el.Space])
 );

Then you can just go over your elems:

 var els = document.querySelectorAll("path");
 els.forEach(function(el){
  if(el.className.split(" ").some(clas=>spaces.has(clas))){
   //do sth
  }
 });

5 Comments

@kind user youre not the only one ;) stackoverflow.com/questions/45439408/…
hey man, nice to see you again :) Thanks for this, what I am trying to do is basically i have a map with polygons and each polygon is a path, with a class as nation names, so if we match a country within the json, I should apply an active class to the path
@rob.m train station?
sort of... just a bit complex thing over here, lets put this way
@rob.m so results is one path? And wheres the html involved??
0

This is the way I would go:

var results = [
  {
    "Title": "Battle of Baekgang",
    "Space": ["South Korea"]
  },
  {
    "Title": "Victory Tests",
    "Space": ["United Kingdom"]
  },
  {
    "Title": "Everett massacre",
    "Space": ["United States"]
  },
  {
    "Title": "Bologna massacre",
    "Space": ["Italy"]
  },
  {
    "Title": "Milano massacre",
    "Space": ["Italy"]
  }
];

var countries = ["Italy"];

for(var i = 0; i < results.length; i++) {
  if(countries.includes(results[i]["Space"][0])) { // the [0] is because of Space being an Array
    console.log(results[i]["Title"]);
  }
}

1 Comment

i have a map with polygons and each polygon is a path, with a class as nation names, so if we match a country within the json, I should apply an active class to the path
0

Below ES6 solution to find all entries that has Space within the countries array

var countries = ["Italy", "United Kingdom", "South Korea"];
var regex = new RegExp(countries.join("|"), "i");
var results = [
  {
        "Title": "Battle of Baekgang",
        "Space": ["South Korea"]
    },
    {
        "Title": "Victory Tests",
        "Space": ["United Kingdom"]
    },
    {
        "Title": "Everett massacre",
        "Space": ["United States"]
    },
    {
        "Title": "Bologna massacre",
        "Space": ["Italy"]
    },
    {
        "Title": "Milano massacre",
        "Space": ["Italy"]
    }
];

let matches = results.reduce((acc,{Space}) => regex.test(...Space)?acc.concat(...Space):acc, []);

matches.forEach((i) => {
jQuery(`path.${i.toLowerCase()}`).addClass('active')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<path class="italy france germany">
</path>
<path class="should not be updated">
</path>

4 Comments

thanks man, i have a map with polygons and each polygon is a path, with a class as nation names, so if we match a country within the json, I should apply an active class to the path, this above would give me a json
The active class is the Space value ? and do you want the 1st match only or all of them ? if all of them, do you want array back ?
what's the deal with ES6 I see it everywhere... From what I understand it's suppose to be an enhancement of JS syntax... Why isn't the community full embracing it though ?
@JasonKrs it is way beyond embracing. ES6 is the next major release of JavaScript!! You need to use google to get more information about this.

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.