0

before anything i've been checking this post : How can I find matching values in two arrays? But it did not help me. I don't really get it. So maybe if I explain my problem here someone can help me to solve it.

I have a school project (I am supposed to match user regarding their location, tags , popularity score etc. Before using an algorithm for the two location I want a first non precise sort. So basically I want to compare two value like 'Paris' and 'Paris')

My search function returns me an [] with two {} inside, so there are two people that match regarding my first search values (gender)

I'm using Node.js, my database is MySQL and my function are using callback and promises so far.

So This is my first object (it contains my searcher's informations)

[ RowDataPacket {
id: 34,
username: 'natedogg',
latitude: 48.8835,
longitude: 2.3219,
country: 'France',
city: 'Paris',
zipcode: 75017 } ]

This is the second one (it contains 2 users)

[ RowDataPacket {
id: 33,
username: 'pablito',
latitude: 48.8921,
longitude: 2.31922,
country: 'France',
city: 'Paris',
zipcode: 75017 },
RowDataPacket {
id: 35,
username: 'tupac',
latitude: 48.8534,
longitude: 2.3488,
country: 'France',
city: 'levallois',
zipcode: 92300 } ]

No that I have this data how can I check if my searcher's location is == my otherUsersLocation (even if they are more than 1 location) Thank for your help !

Community
  • 1
  • 1
pkerckhove
  • 763
  • 2
  • 10
  • 17
  • I tried `firstArray[0].city == secondArray[0].city.` But here I have thwo .city in the second array so it will match for the first one but not the second one. – pkerckhove Jan 11 '17 at 09:36
  • @Cruiser I've also tried the solution further down but it doesn't work. I did `for (city in potentialsLocation) { for (city in searcherLoc) { if (city === searcherLoc[0].[city]) { console.log("working ?"); } } }` I've modified the value in my db so it should work but no I can't log anything – pkerckhove Jan 11 '17 at 10:15

2 Answers2

0

I worked up some code for you, that works and you should be able to apply it to your situation. Basically, we are just looping through the objects we want to search and looking at the "city" property. I renamed some of the variables to make what's going on clearer.

var data = {
    id: 34,
    username: 'natedogg',
    latitude: 48.8835,
    longitude: 2.3219,
    country: 'France',
    city: 'Paris',
    zipcode: 75017 
};

var searchable = [{
    id: 33,
    username: 'pablito',
    latitude: 48.8921,
    longitude: 2.31922,
    country: 'France',
    city: 'Paris',
    zipcode: 75017 },
    {
    id: 35,
    username: 'tupac',
    latitude: 48.8534,
    longitude: 2.3488,
    country: 'France',
    city: 'levallois',
    zipcode: 92300 
}];

// this array is just to save obj when we have a match
var found =[];
// loop through each element in our searchable array
for(var i=0;i<searchable.length;++i){
    if(data["city"] == searchable[i]["city"]){
       // did we find it? push it onto the found array
       console.log("found: " + searchable[i]["city"]);
       found.push(searchable[i]["username"]);}
}
// look to see if we got what we wanted
console.log(found);

You can follow this same procedure to match anything in the data.

Cruiser
  • 1,618
  • 2
  • 16
  • 20
  • Thank you ! That is exactly what I was looking for ! I managed to fix my problem this afternoon with the solution I just posted a few minutes ago. But i'll keep yours in mind because I will use this idea again – pkerckhove Jan 11 '17 at 14:39
  • Hey ! I just wanted to tell you that you idea is working well. Thank you for your help. Im doing something similar today but it doesn't work, basically it's the same idea I want to find some matching value between two array but I can't loop properly on my array values. If you could help me about this one as well it would be great ! – pkerckhove Jan 12 '17 at 17:34
0

This is how I managed to find the matching values in my arrays if (SecondArray){ // console.log(SecondArray.length); SecondArray.forEach(function(element) { if (firstArray[0].city === element.city) { console.log("element); } }) }

pkerckhove
  • 763
  • 2
  • 10
  • 17