2017-01-10 52 views
0

之前我一直在检查这个帖子:How can I find matching values in two arrays? 但它没有帮助我。我真的不明白。所以,如果我在这里解释我的问题,那么有人可以帮助我解决它。在JavaScript数组中匹配值

我有一个学校项目(我应该匹配用户有关他们的位置,标签,流行乐谱等 在使用两个位置的算法之前,我想要一个第一个非精确排序,所以基本上我想比较两个值,如“巴黎”和“巴黎”)

我的搜索函数返回我有两个{}内的[],所以有关于符合我的第一个搜索值的两个人(性别)

我使用Node.js,我的数据库是MySQL,并且我的功能目前使用回调和承诺。

所以这是我的第一个对象(它包含我的搜索者的信息)

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

这是第二个(包含2个用户)

[ 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 } ] 

无我有这个数据怎么能我检查我的搜索者的位置是否是我的otherUsersLocation(即使它们超过1个位置) 感谢您的帮助!

+0

我试过'firstArray [0] ==。市secondArray [0] .city.'但在这里我有thwo。城市的第二个数组,所以它会匹配第一个但不是第二个。 – pkerckho

+0

@Cruiser我也尝试了解决方案,但它不起作用。我做了 'for(city in potentialsLocation){ for(cities in searcherLoc){ if(cities === searcherLoc [0]。[city]){ console.log(“working?”); } } }' }我修改了数据库中的值,所以它应该可以工作,但是我不能记录任何内容 – pkerckho

回答

1

这听起来像你基本上想要比较你的第一个对象(你的搜索数据)和第二个数组(你的用户数据)中的对象。您不一定需要直接比较两个数组,因为您的数据比字符串或数字更复杂。

为了比较普通的JavaScript我会做这样的事情的对象:

// loop through each object in user array 
for (object in secondArray) { 

    // loop through each property in the user object 
    for (property in object) { 

     // compare the property of user object with corresponding search object property 
     if (property === firstArray[0][property]) { 
      // do something 
     } 
    } 
} 
+0

嗨,谢谢你的回答,我试过了,但是它返回一个错误:firstArray [0]。[property],意外的标记['我真的不知道为什么 – pkerckho

+0

@pkerckho我编辑了我的答案。我需要说'firstArray [0] [property]'而不是'firstArray [0]。[property]'。希望它现在可以工作!当我在浏览器控制台中设置它时,它确实适合我! –

+1

@Tomas Juranek,我是这样解决的:'if(SecondArray){console.log(SecondArray.length); SecondArray.forEach(函数(元件){ 如果(firstArray [0]。市=== element.city){ 的console.log(“元件); } }) }' 我也将尝试你想法,谢谢你的帮助! – pkerckho

0

我曾经为你一些代码,那工作,你应该能够将它应用到你的情况。基本上,我们只是循环浏览想要搜索的对象并查看“城市”属性。我重新命名了一些变量,以使得事情更清晰。

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

您可以按照相同的步骤来匹配数据中的任何内容。

+0

谢谢!这就是我正在寻找的!我今天下午用我刚刚发布的解决方案解决了我的问题,但我会记住你的想法,因为我会再次使用这个想法 – pkerckho

+0

嘿!我只是想告诉你,你的想法运作良好。谢谢你的帮助。我今天做了类似的事情,但它不起作用,基本上它是一样的想法,我想找到一些匹配的价值在两个数组之间,但我无法正确地循环我的数组值。如果你可以帮我介绍一下这个t会很棒! – pkerckho

0

这是我设法找到我的阵列中的匹配值 if (SecondArray){ // console.log(SecondArray.length); SecondArray.forEach(function(element) { if (firstArray[0].city === element.city) { console.log("element); } }) }