我得到的是空数组怎么了?

时间:2019-12-20 21:35:09

标签: javascript arrays

我希望名称中包含字符串“ Uni”的大学。然后,创建一个名为uni的新数组,其中包含所有数组。我使用HOF方法仍然无法正常工作。

var graduates = [{ id: 4, first_name: "Modestine", university: "International Medical & Technological University", email: "mdolder3@symantec.com" },
  { id: 5, first_name: "Chick", university: "Sultan Salahuddin Abdul Aziz Shah Polytechnic", email: "camorts4@google.com.au" },
  { id: 6, first_name: "Jakob", university: "Fachhochschule Rosenheim, Hochschule für Technik und Wirtschaft", email: "jharken5@spiegel.de" },
  { id: 7, first_name: "Robbi", university: "Salem University", email: "rbrister6@redcross.org" },
  { id: 8, first_name: "Colline", university: "Coastal Carolina University", email: "cbrosh7@alibaba.com" },
  { id: 9, first_name: "Michail", university: "Universidad Católica de Ávila", email: "mrome8@shinystat.com" },
  { id: 10, first_name: "Hube", university: "Universitat Rovira I Virgili Tarragona", email: "hlethbrig9@foxnews.com" }]

const unisWithUni = [];
console.log(unisWithUni);
for (let i = 0; i < graduates.length; i++) {
  if (graduates[i].university === "Uni") {
    unisWithUni.push(graduates[i]);
  }
}

3 个答案:

答案 0 :(得分:1)

您正在比较一个完整的字符串。在javascript(或几乎所有语言)中,“等于”并不意味着“包含”。所以"hi" === "hi what's up"是不一样的,它们是不相等的。

在JavasSript中,您要查找indexOf()(旧的和新的浏览器)或includes()(新的浏览器),所以:

 var graduates = [
     { id: 4, first_name: "Modestine", university: "International Medical & Technological University", email: "mdolder3@symantec.com" },
    { id: 5, first_name: "Chick", university: "Sultan Salahuddin Abdul Aziz Shah Polytechnic", email: "camorts4@google.com.au" },
    { id: 6, first_name: "Jakob", university: "Fachhochschule Rosenheim, Hochschule für Technik und Wirtschaft", email: "jharken5@spiegel.de" },
    { id: 7, first_name: "Robbi", university: "Salem University", email: "rbrister6@redcross.org" },
    { id: 8, first_name: "Colline", university: "Coastal Carolina University", email: "cbrosh7@alibaba.com" },
    { id: 9, first_name: "Michail", university: "Universidad Católica de Ávila", email: "mrome8@shinystat.com" },
    { id: 10, first_name: "Hube", university: "Universitat Rovira I Virgili Tarragona", email: "hlethbrig9@foxnews.com"}
];

const unisWithUni = [];
for (let i = 0; i < graduates.length; i++) {
  if (graduates[i].university.indexOf("Uni") > -1) {
    unisWithUni.push(graduates[i]);
  }
}
console.log(unisWithUni);

答案 1 :(得分:1)

您应该以这种方式将graduates[i].university === "Uni"更改为graduates[i].university.includes("Uni"),以便找到任何包含“ Uni”的字符串,否则它将仅与“ Uni”完全匹配。

另外,将console.log(unisWithUni);放在for循环之后。

var graduates = [{ id: 4, first_name: "Modestine", university: "International Medical & Technological University", email: "mdolder3@symantec.com" },
  { id: 5, first_name: "Chick", university: "Sultan Salahuddin Abdul Aziz Shah Polytechnic", email: "camorts4@google.com.au" },
  { id: 6, first_name: "Jakob", university: "Fachhochschule Rosenheim, Hochschule für Technik und Wirtschaft", email: "jharken5@spiegel.de" },
  { id: 7, first_name: "Robbi", university: "Salem University", email: "rbrister6@redcross.org" },
  { id: 8, first_name: "Colline", university: "Coastal Carolina University", email: "cbrosh7@alibaba.com" },
  { id: 9, first_name: "Michail", university: "Universidad Católica de Ávila", email: "mrome8@shinystat.com" },
  { id: 10, first_name: "Hube", university: "Universitat Rovira I Virgili Tarragona", email: "hlethbrig9@foxnews.com" }]

const unisWithUni = [];
  for (let i = 0; i < graduates.length; i++) {
    if (graduates[i].university.includes("Uni")) {
      unisWithUni.push(graduates[i]);
    }

}

console.log(unisWithUni);

答案 2 :(得分:-1)

以下是使用jQuery的map()的示例。为什么,只是因为。

请注意,在搜索字符串uni之前,我将大学转换为小写形式。

var graduates = [{
    id: 4,
    first_name: "Modestine",
    university: "International Medical & Technological University",
    email: "mdolder3@symantec.com"
  },
  {
    id: 5,
    first_name: "Chick",
    university: "Sultan Salahuddin Abdul Aziz Shah Polytechnic",
    email: "camorts4@google.com.au"
  },
  {
    id: 6,
    first_name: "Jakob",
    university: "Fachhochschule Rosenheim, Hochschule für Technik und Wirtschaft",
    email: "jharken5@spiegel.de"
  },
  {
    id: 7,
    first_name: "Robbi",
    university: "Salem University",
    email: "rbrister6@redcross.org"
  },
  {
    id: 8,
    first_name: "Colline",
    university: "Coastal Carolina University",
    email: "cbrosh7@alibaba.com"
  },
  {
    id: 9,
    first_name: "Michail",
    university: "Universidad Católica de Ávila",
    email: "mrome8@shinystat.com"
  },
  {
    id: 10,
    first_name: "Hube",
    university: "Universitat Rovira I Virgili Tarragona",
    email: "hlethbrig9@foxnews.com"
  }
];

var unisWithUni = $.map(graduates, grad => {
  return grad.university.toLowerCase().includes("uni") ? grad : null;
});

console.log(unisWithUni);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>