如何检查两个数组是否包含相同的值,即使它们位于不同的索引中?

时间:2016-12-22 21:46:38

标签: javascript angularjs node.js

我正在从事一个个人项目,而且我在最后一部分是残留的。我正在尝试仅为我公司数据库中公司名称相同的公司获取数据。

我最终的目标是将从以下两个调用中获得的两个jsons合并为一个

    Call one: $http.get('//localhost:8081/api/jobs').then(function(res) 
    Call two: localhost:8081/api/glassdoor/

完整代码:

    $http.get('//localhost:8081/api/jobs').then(function(res) {
    $scope.data = res.data; //data from the database
    $scope.size = $scope.data.length; //length 132
    for (var i = 0; i < $scope.size; i++) {
        if ($scope.data[i].glassdoor !== null && $scope.data[i].glassdoor !== undefined) {
            $scope.hasGlassdoor = [];
            for (var i = 0; i < $scope.size; i++) {
                if ($scope.data[i].glassdoor !== null && $scope.data[i].glassdoor !== undefined)
                    $scope.hasGlassdoor.push($scope.data[i]);
            }

            //Get the companies name that have glassdoor
            $scope.company = [];
            for (var j = 0; j < $scope.hasGlassdoor.length; j++) {
                $scope.company.push($scope.hasGlassdoor[j].company);
            }

            //Create the URL calls for my glassdoor api
            $scope.url = [];
            for (var x = 0; x < $scope.company.length; x++) {
                $scope.url.push('//localhost:8081/api/glassdoor/' + $scope.company[x]);
            }
            //For example : '//localhost:8081/api/glassdoor/A9.com'

            //Get the Glassdoor data
            var company = $scope.company;
            for (var j = 0; j < $scope.url.length; j++) {
                $http.get($scope.url[j]).then(function(response) {
                    $scope.gData = response.data;
                    $scope.gSize = $scope.gData.length;
                    $scope.gName = [];

                    //Get the names of the companies that glassdoor returns
                    for(var x = 0; x < $scope.gSize; x++){
                         if ($scope.gData[x] !== null && $scope.gData[x] !== undefined) {
                            if ($scope.gData[x].name !== null && $scope.gData[x].name !== undefined) {
                                    $scope.gName.push($scope.gData[x].name);
                            }
                         }
                    }
                    //Now I'm trying to only get the names of the companies that are in my variable company
                    //For example '//localhost:8081/api/glassdoor/6sense
                    //returns data for 6sense, and a company named 6sense Technologies
                    //I'm trying to just get the data of 6sense

                    //
                    //      TODO
                    //


                    // My try using loDash returns undefined.
                    // I'm trying to see if $scope.gName is in var Company.
                    //if(_.includes(company, $scope.gName)){
                    //    gd.push($scope.gName);
                    //}

                }); //this is what is calling the glassdoor api
            }//end of the for loop for url.
        } //if statement to check for null
    } //first for loop
}).catch(function(error, res) {
    console.log("Error:", error, res);
});

现在我正在处理小清单,所以我可以解决这个问题。

我最终的目标是将此作为我完成的json:

[  
   {  
      "company": "23andMe"
       "glassdoor":"https://www.glassdoor.com/Overview/Working-at-23andMe-EI_IE145899.11,18.htm"

"img":"https://www.23andme.com/static/img/icons/logo_alt.96cf7888b73d.svg"
       "international":null
       "link":"https://www.23andme.com/careers/"
       "location":"Mountain View, CA"
       "secondary":null
       "third":null
      "id":145899,
      "name":"23andMe",
      "website":"www.23andme.com",
      "isEEP":true,
      "exactMatch":true,
      "industry":"Biotech & Pharmaceuticals",
      "numberOfRatings":27,
      "squareLogo":"https://media.glassdoor.com/sqll/145899/23andme-squarelogo.png",
      "overallRating":"4.2",
      "ratingDescription":"Very Satisfied",
      "cultureAndValuesRating":"4.5",
      "seniorLeadershipRating":"3.6",
      "compensationAndBenefitsRating":"4.0",
      "careerOpportunitiesRating":"3.4",
      "workLifeBalanceRating":"4.3",
      "recommendToFriendRating":80,
      "sectorId":10005,
      "sectorName":"Biotech & Pharmaceuticals",
      "industryId":200021,
      "industryName":"Biotech & Pharmaceuticals",
      "featuredReview":{  
         "attributionURL":"https://www.glassdoor.com/Reviews/Employee-Review-23andMe-RVW11447587.htm",
         "id":11447587,
         "currentJob":true,
         "reviewDateTime":"2016-08-03 15:05:20.157",
         "jobTitle":"Customer Care Reporesentative",
         "location":"Mountain View, CA",
         "jobTitleFromDb":"Customer Care Reporesentative",
         "headline":"Customer Care Representative",
         "pros":"the environment-everyone is extremely genuine, smart, and friendly. management is very understanding and open. Executives are transparent with everything going on in the company\r\nbenefits-free gym, food every day, snacks, great health coverage, rooftop access, etc\r\nworkspace-facilities does a phenomenal job at keeping everything extremely clean and fixes all issues ASAP. I don't feel like I'm sitting a boring desk job all day, it's a fun place to be",
         "cons":"Traffic through downtown mountain view can suck and the train can be kind of loud (I cannot think of a legitimate con, everything is awesome here)",
         "overall":5,
         "overallNumeric":5
      },
      "ceo":{  
         "name":"Anne Wojcicki",
         "title":"CEO",
         "numberOfRatings":15,
         "pctApprove":100,
         "pctDisapprove":0
      }
   }
]

处理glassdoor调用的Server.js:

//Glassdoor api call
app.get('/api/glassdoor/:company', function(req, res, next) {
    var company = req.params.company;
    requestify.get('https://api.glassdoor.com/api/api.htm?t.p=PRODUCT&t.k=KEY&userip=0.0.0.0&useragent=&format=json&v=1&action=employers&q=' + company).then(function(response) {
        // Get the response body (JSON parsed or jQuery object for XMLs)
        gData = response.getBody();
        gData = gData.response.employers;
        res.json(gData);
    });



});

2 个答案:

答案 0 :(得分:0)

对数组进行排序,然后枚举它。如果以前===当前你有一个骗局。

答案 1 :(得分:0)

这就是我解决它的方法。

app.get('/api/glassdoor/:company', function(req, res, next) {
    var company = req.params.company;
    requestify.get('https://api.glassdoor.com/api/api.htm?t.p=PRODUCT&t.k=KEY&userip=0.0.0.0&useragent=&format=json&v=1&action=employers&q=' + company).then(function(response) {
        // Get the response body (JSON parsed or jQuery object for XMLs)
        gData = response.getBody();
        gData = gData.response.employers;
        //What I added to only send the data of the companies that where like 'company'
        for (var i = 0; i < gData.length; i++) {
            if (gData[i].name === company) {
                gData = gData[i];
                res.send(gData);
            }
        }
    });
});

哪个不回答整个问题,但这就是我解决我的特定问题的方法。