简体   繁体   English

匹配 javascript 数组中的值

[英]Matching values in javascript arrays

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.我正在使用 Node.js,我的数据库是 MySQL,到目前为止我的函数正在使用回调和承诺。

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)这是第二个(它包含 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 } ]

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 !不,我有这些数据,我如何检查我的搜索者的位置是否 == 我的 otherUsersLocation(即使它们超过 1 个位置)谢谢您的帮助!

It sounds like you basically want to compare your first object (your search data), against your objects in the second array (your user data). 听起来您基本上是想将第一个对象(您的搜索数据)与第二个数组中的对象(您的用户数据)进行比较。 You don't necessarily directly need to compare the two arrays because you have more complicated data than things like strings or numbers. 您不必直接比较两个数组,因为您拥有比字符串或数字之类的数据更复杂的数据。

To compare the objects in plain JavaScript I would do something like this: 为了比较普通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
        }
    }
}

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. 基本上,我们只是遍历要搜索的对象并查看“ city”属性。 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. 您可以按照相同的过程来匹配数据中的任何内容。

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); } }) } if (SecondArray){ // console.log(SecondArray.length); SecondArray.forEach(function(element) { if (firstArray[0].city === element.city) { console.log("element); } }) }这就是我设法在数组中找到匹配值的方法if (SecondArray){ // console.log(SecondArray.length); SecondArray.forEach(function(element) { if (firstArray[0].city === element.city) { console.log("element); } }) } if (SecondArray){ // console.log(SecondArray.length); SecondArray.forEach(function(element) { if (firstArray[0].city === element.city) { console.log("element); } }) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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