Javascript 如何检查两个数组是否包含相同的值,即使它们位于不同的索引中?
我正在做一个个人项目,最后一部分我是stump。我试图只获取与我数据库中的公司同名的公司的数据 最后,我的目标是将从以下两个调用中获得的两个JSON合并为一个Javascript 如何检查两个数组是否包含相同的值,即使它们位于不同的索引中?,javascript,angularjs,node.js,Javascript,Angularjs,Node.js,我正在做一个个人项目,最后一部分我是stump。我试图只获取与我数据库中的公司同名的公司的数据 最后,我的目标是将从以下两个调用中获得的两个JSON合并为一个 Call one: $http.get('//localhost:8081/api/jobs').then(function(res) Call two: localhost:8081/api/glassdoor/ 完整代码: $http.get('//localhost:8081/api/jobs').the
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);
});
处理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);
});
});
对数组排序,然后枚举它。如果previous===current,则有一个重复。对数组排序,然后枚举它。如果previous===current,您就有一个重复。这就是我解决它的方法
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);
}
}
});
});
app.get('/api/glassdoor/:company',函数(req,res,next){
var公司=所需参数公司;
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=“+公司)。然后(功能(响应){
//获取响应主体(JSON解析或XMLs的jQuery对象)
gData=response.getBody();
gData=gData.response.employers;
//我添加的内容仅发送喜欢“公司”的公司的数据
对于(变量i=0;i
这并不能回答整个问题,但这就是我如何解决我的特殊问题。这就是我如何解决的
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);
}
}
});
});
app.get('/api/glassdoor/:company',函数(req,res,next){
var公司=所需参数公司;
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=“+公司)。然后(功能(响应){
//获取响应主体(JSON解析或XMLs的jQuery对象)
gData=response.getBody();
gData=gData.response.employers;
//我添加的内容仅发送喜欢“公司”的公司的数据
对于(变量i=0;i
这并不能回答整个问题,但我就是这样解决我的问题的