Reputation: 1926
Currently I have three arrays. AppNames
which is an application Name. Number of AddOns
which is the number of add ons each application is using. (Both of these arrays are used together. E.G "cirr-contentful-demo" has 1 add on).
Look Below:
var prodArrayAppName = [],
stgArrayAppName = [],
devArrayAppName = [],
prodNoAddOns = [],
stgNoAddOns = [],
devNoAddOns = [];
var appNames = ["cirr-contentful-demo", "cirr-contentful-handler-backup", "cirr-test-app"];
var numberAddOne = [1, 5, 7]
var production = [{
"id": "16",
"heroku_application": "cirr-contentful-demo",
"stage": "Production"
},
{
"id": "4",
"heroku_application": "cirr-contentful-handler-backup",
"stage": "Staging"
},
{
"id": "9",
"heroku_application": "test-backup",
"stage": "Development"
}];
What I need is to loop through the production
array of objects, match the heroku_application
name to the name in the appNames
array. Once found check the stage type example: Production, Staging, Development.
The push the application name to the correct array. E.G Production applications to prodArrayAppName
. Then grab the number of addOns that application has and put it into the correct NoOfAddons
. E.G prodArrayAppName.
Meaning the end game should look like:
prodArrayAppName = [cirr-contentful-demo]
prodNoAddOns = [1]
stgArrayAppName = [cirr-contentful-handler-backu]
stgNoAddOns = [5]
devArrayAppName = [test-backup]
devNoAddOns = [7]
This Is the code I have tried so far, but so far getting no luck:
production.forEach(function(a) {
appNames.forEach(function(b) {
numberAddOne.forEach(function(c) {
if (a === b.heroku_application) {
if (b.stage === "Production") {
prodArrayAppName.push(b.heroku_application);
prodNoAddOns.push(c);
} else if (b.stage === "Staging") {
stgArrayAppName.push(b.heroku_application);
stgNoAddOns.push(c);
} else {
devArrayAppName.push(b.heroku_application);
devNoAddOns.push(c);
}
}
});
});
});
Upvotes: 0
Views: 184
Reputation: 2060
The code you have tried has some problems.The condition check you used if (a === b.heroku_application) itself is wrong.Try the code given below
var prodArrayAppName = [], stgArrayAppName = [], devArrayAppName = [], prodNoAddOns = [],
stgNoAddOns = [], devNoAddOns = [];
var appNames = ["cirr-contentful-demo", "cirr-contentful-handler-backup", "cirr-test-app"];
var numberAddOne = [1,5,7];
var production = [
{
"id":"16",
"heroku_application":"cirr-contentful-demo",
"stage" : "Production"
},
{
"id":"4",
"heroku_application":"cirr-contentful-handler-backup",
"stage" : "Staging"
},
{
"id":"9",
"heroku_application":"test-backup",
"stage" : "Development"
}];
production.forEach(function(a) {
for(var i=0;i<appNames.length;i++)
{
if(a.heroku_application==appNames[i])
{
if(a.stage==="Production")
{
prodArrayAppName.push(appNames[i]);
prodNoAddOns.push(numberAddOne[i]);
}
else if(a.stage==="Staging")
{
stgArrayAppName.push(appNames[i]);
stgNoAddOns.push(numberAddOne[i]);
}
else if(a.stage==="Development")
{
devArrayAppName.push(appNames[i]);
devNoAddOns.push(numberAddOne[i]);
}
}
}
});
//checking code
prodArrayAppName.forEach(function(a) {
alert(a);
});
Upvotes: 1
Reputation: 136104
You dont need explicit loops at all to acomplish this - make use of reduce
:
var init = {
DevelopmentAppName:[],
DevelopmentNoAddOns:[],
StagingAppName:[],
StagingNoAddOns:[],
ProductionAppName:[],
ProductionNoAddOns:[],
};
var appNames = ["cirr-contentful-demo", "cirr-contentful-handler-backup", "cirr-test-app"];
var numberAddOne = [1,5,7]
var production = [
{
"id":"16",
"heroku_application":"cirr-contentful-demo",
"stage" : "Production"
},
{
"id":"4",
"heroku_application":"cirr-contentful-handler-backup",
"stage" : "Staging"
},
{
"id":"9",
"heroku_application":"test-backup",
"stage" : "Development"
}];
var result = production.reduce(function(p,c){
var idx = appNames.indexOf(c.heroku_application);
p[c.stage + 'AppName'].push(c.heroku_application);
p[c.stage + 'NoAddOns'].push(numberAddOne[idx] || 0);
return p;
}, init);
console.log(result);
Upvotes: 1
Reputation: 2152
You don't really need nested loops.
production
:
heroku_application
in appNames
. We'll use that to find the number.stage
, add the project's name to (stage)ArrayAppName, and the project's cross-referenced number to (stage)NoAddOns.production.forEach(function(project) {
var index = appNames.indexOf(project.heroku_application);
if (index === -1) { return; }
switch (project.stage) {
case "Production":
prodArrayAppName.push(project.heroku_application);
prodNoAddOns.push(numberAddOne[index]);
break;
case "Staging":
stgArrayAppName.push(project.heroku_application);
stgNoAddOns.push(numberAddOne[index]);
break;
case "Development":
devArrayAppName.push(project.heroku_application);
devNoAddOns.push(numberAddOne[index]);
break;
}
});
Upvotes: 1