使用for循环和if语句检查两个数组

时间:2016-10-31 00:44:47

标签: javascript arrays if-statement for-loop

我正在尝试对两个数组运行检查(一个有4个对象,一个只有几个字符串),带有for循环和if语句用于问题集。

这个想法是使用for循环迭代对象数组和字符串数组中的每个元素,然后使用if语句来计算匹配并将匹配的字符串推送到新数组中。迭代完所有元素后,如果存在匹配的字符串,则返回该字符串。

问题是,只要找到对象数组中的单个匹配项,函数就会调用它,并仅返回该对象而不是迭代对象数组中的其余元素。

var passengers = [
    { name: ["Michael Jackson"], paid: true }, 
    { name: ["Osama"], paid: false }, 
    { name: ["Harambe"], paid: true },
    { name: ["Pepe"], paid: true },
];

var noFlyList = ["Jimmy", "John", "Pepe", "Osama"];

function checkNoFly(passengers, noFlyList) {
    for (var i = 0; i < passengers.length; i++) {
        for (var j = 0; j < noFlyList.length; j++) {
            if (passengers[i].name[0] == noFlyList[j]) {
                var passengerList = [];
                passengerList.push(passengers[i].name[0]);
                return passengerList;
            }
        }
    }
    return true;
}

function checkNotPaid(passengers) {
	return (!passengers.paid);
}

function processPassenger(passengers, testFunction) {
    for (var i = 0; i < passengers.length; i++) {
        if (testFunction(passengers[i])) {
            return false;
        }
    }
    return true;
}

var allCanFly = processPassenger(passengers, checkNoFly);
if (!allCanFly) {
    console.log("We cannot fly because " + checkNoFly(passengers, noFlyList) + " is on the no-fly list");
}

var allPaid = processPassenger(passengers, checkNotPaid);
if (!allPaid) {
	console.log("we cannot fly because not all passengers have paid");
}

2 个答案:

答案 0 :(得分:0)

使用此:在循环中使用passengerList使其在每个循环上重新初始化为空数组,并且在循环中返回passengerList会在循环完成第一个循环后使循环中断

function checkNoFly(passengers, noFlyList) {
    var passengerList = [];
    for (var i = 0; i < passengers.length; i++) {
        for (var j = 0; j < noFlyList.length; j++) {
            if (passengers[i].name[0] == noFlyList[j]) {

                passengerList.push(passengers[i].name[0]);

            }
        }
    }
    return passengerList;
}

修改

将原始的processPassenger函数更改为下面的函数,原来你只有一个参数传递给你的checkNoFly函数,你在其中定义了2个参数,所以对于错误的参数数量返回false,这会阻止你从你想要的方式得到它。

function processPassenger(passengers, testFunction) {

    if (testFunction(passengers, noFlyList).length !=0) {
        return false;
    }

    return true;
}

编辑2 :对于您更新的问题,因为对于第一次检查,我们返回一个数组用于单个函数processPassenger()进行验证,我们可以对{{1函数为那些没有付款的人返回一个数组。

checkNotPaid
除非你想重构一切,否则我认为这样可以。

答案 1 :(得分:0)

你告诉它这样做:return passengerList;在你的内循环中。此外,您继续在内部for循环中重新声明变量var passengerList = [];,每次都清空它。

&#13;
&#13;
var passengers = [
  { name: ["Michael Jackson"], paid: true }, 
  { name: ["Osama"], paid: false }, 
  { name: ["Harambe"], paid: true },
  { name: ["Pepe"], paid: true },
];

var noFlyList = ["Jimmy", "John", "Pepe", "Osama"];

function checkNoFly(passengers, noFlyList) {
  var passengerList = [];
  for (var i = 0; i < passengers.length; i++) {
    for (var j = 0; j < noFlyList.length; j++) {
      if (passengers[i].name[0] == noFlyList[j]) {
        passengerList.push(passengers[i].name[0]);
      }
    }
  }
  return passengerList;
}
function checkNoPay(passengers) {
  var nonPayers = [];
  for (var i = 0; i < passengers.length; i++) {
    if (!passengers[i].paid) { nonPayers.push(passengers[i].name); }
  }
  return nonPayers;
}

var banList = checkNoFly(passengers, noFlyList);
if (banList.length) {
  console.log("We cannot fly because " + banList + " is/are on the no-fly list");
}
var unpaidList = checkNoPay(passengers);
if (unpaidList.length) {
  console.log("We cannot fly because " + unpaidList + " has/have not payed the flight");
}
var canWeFly = !(banList.length || unpaidList.length);
console.log(canWeFly ? "We can fly" : "We cannot fly");
&#13;
&#13;
&#13;