我一直在获取输出,但它是两倍的数量。例如,如果所需的输出是SUGAR,则相似。程序将显示
SUGAR
SUGAR
无论如何我能解决这个问题吗?我实际上是使用嵌套的for循环遍历一个对象数组。下面是嵌套for循环的代码
for(var i=0; i<ingredientList.length; i++){
for(var j=0; j<this.objUser.length; j++){
len = this.objUser.length;
userAllergyDetails.push(this.objUser[j].userAllergies);
for(var k=0; k<len; k++){ //matching starts
if(ingredientList[i] == this.objUser[j].userAllergies.toUpperCase()){
console.log('match');
unSafeResult.push(ingredientList[i]);
console.log(unSafeResult);
}
}
}
}
console.log(userAllergyDetails);
console.log(userAllergyDetails.length);
console.log(this.objUser);
}
上述代码的输出是
ingredient list (26) ["INGREDIENTS↵WHEAT FLOUR", "SUGAR", "CHOCOLATE LIQUOR", "HYDROGENATED VEGETABLE↵OIL", "SAL FAT", "SHEA BUTTER", "RAPESEED OIL", "PALM OIL", "SUNFLOWER OIL", "WHOLE MILK↵POWDER", "SHORTENING", "COCOA BUTTER", "SALT", "BUTTER", "YEAST", "EMULSIFIER", "POLYGLYCEROLPOLYRICINOLEANTE", "SOY LECITHIN", "ARTIFICIAL FLAVOR", "TRISODIUMPHOSPHATE", "LEAVENING", "SODIUM BICARBONATE", "CONTAINS: MILK", "WHEAT", "SOYBEANS and SHEANUT", "Manufactured in a facility that uses↵EGGS and PEANUTS"]
match
main.js:41963 ["SUGAR"]0: "SUGAR"1: "SUGAR"length: 2__proto__: Array(0)
main.js:41961 match
main.js:41963 (2) ["SUGAR", "SUGAR"]0: "SUGAR"1: "SUGAR"length: 2__proto__: Array(0)
main.js:41968 data retrieve from object (52) ["Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar"]
这是我试图循环的对象
Object
0:
resultUnsafe
:
Array(0)
resultWarning
:
Array(0)
userAllergies
:
"Oil"
userName
:
"Dad"
__proto__
:
Object
1
:
Object
resultUnsafe
:
Array(0)
resultWarning
:
Array(0)
userAllergies
:
"Sugar"
userName
:
"Mum"
如对象所示,只有2个userAllergies,这意味着长度为2但是为什么它在控制台中显示52?以及如何确保只将SUGAR推送到阵列?
答案 0 :(得分:1)
为了使您的代码更好,您应该考虑使用更具描述性的索引变量:
@Configuration
public class QuartzBeans {
// frequencyInSec can be read through property file of can be set setter
// method.
int frequencyInSec = 10;
public void setFrequencyInSec(int frequencyInSec) {
this.frequencyInSec = frequencyInSec;
}
@Autowired
private ApplicationContext applicationContext;
/*
* @PostConstruct public void init() {
* logger.info("Hello world from Spring..."); }
*/
Logger logger = LoggerFactory.getLogger(getClass());
@Bean
public SpringBeanJobFactory springBeanJobFactory() {
AutoWiringSpringBeanJobFactory jobFactory = new AutoWiringSpringBeanJobFactory();
logger.debug("Configuring Job factory");
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
@Bean
public SchedulerFactoryBean scheduler(Trigger[] trigger, JobDetail[] job) {
SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
schedulerFactory.setConfigLocation(new ClassPathResource("quartz.properties"));
logger.debug("Setting the Scheduler up");
schedulerFactory.setJobFactory(springBeanJobFactory());
schedulerFactory.setJobDetails(job);
schedulerFactory.setTriggers(trigger);
return schedulerFactory;
}
@Bean(name = "jobb")
@Scope("prototype")
public JobDetailFactoryBean jobDetail() {
JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
jobDetailFactory.setJobClass(SampleJob.class);
jobDetailFactory.setName("Qrtz_Job_Detail");
jobDetailFactory.setDescription("Invoke Sample Job service...");
jobDetailFactory.setDurability(true);
return jobDetailFactory;
}
/*
* @Autowired JobDetailFactoryBean jobDetail1;
*
* @Autowired JobDetailFactoryBean jobDetail;
*
* @Autowired SimpleTriggerFactoryBean trigger2;
*
* @Autowired SimpleTriggerFactoryBean trigger;
*/
@Bean(name = "jobb5")
@Scope("prototype")
public JobDetailFactoryBean jobDetail1() {
JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
jobDetailFactory.setBeanName("bean33");
jobDetailFactory.setJobClass(SampleJob2.class);
jobDetailFactory.setName("Qrtz_Job_Detail2d");
jobDetailFactory.setDescription("Invoke Sample Job service2...");
jobDetailFactory.setDurability(true);
return jobDetailFactory;
}
/*
* @Bean public SimpleTriggerFactoryBean trigger2(JobDetail job) {
*
* SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
* trigger.setJobDetail(job);
*
* int frequencyInSec = 4;
* logger.info("Configuring trigger to fire every {} seconds",
* frequencyInSec);
*
* trigger.setRepeatInterval(frequencyInSec * 1000);
* trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
* trigger.setName("Qrtz_Trigger"); return trigger; }
*/
@Bean(name = "helldo")
public SimpleTriggerFactoryBean trigger(JobDetail job) {
SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
trigger.setJobDetail(job);
logger.info("Configuring trigger to fire every {} seconds", frequencyInSec);
trigger.setRepeatInterval(frequencyInSec * 1000);
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setName("Qrtz_Trigger2");
return trigger;
}
@Bean(name = "helld2")
public SimpleTriggerFactoryBean trigger2(JobDetail job) {
SimpleTriggerFactoryBean trigger2 = new SimpleTriggerFactoryBean();
trigger2.setBeanName("bean1");
trigger2.setJobDetail(job);
logger.info("Configuring trigger to fire every {} seconds", frequencyInSec);
trigger2.setRepeatInterval(frequencyInSec * 1000);
trigger2.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger2.setName("Qrtz_Trigger23");
return trigger2;
}
}
输入需要更长时间,但更容易调试像您所拥有的那些微妙的问题
这是适用的版本,更易于阅读:
for (var ingredientIndex = 0; ingredientIndex < ingredientList.length; ingredientIndex ++)