简体   繁体   English

如何添加到 Javascript 中的函数?

[英]How do I add to a function in Javascript?

I've been going through a bootcamp, and I can't seem to figure out how to add to a function in Javascript, specifically when I've already placed my return value.我一直在经历一个训练营,我似乎无法弄清楚如何在 Javascript 中添加一个函数,特别是当我已经放置了我的返回值时。 For example, this function runs, but if someone said "okay, now if there is no one that matches the studying string, return a message that reads "No matches." Where do I add a new if statement saying "if (newArr.length = 0) {return "No matches.} Desperately needing help on this, and I've got an exam on JS functions tomorrow. Thought I had everything down, until this big ole curveball. Any and all help is greatly appreciated, thank you!例如,此函数运行,但如果有人说“好吧,现在如果没有匹配学习字符串的人,则返回一条消息,内容为“不匹配”。我在哪里添加一个新的 if 语句,说“if (newArr. length = 0) {return "No matching.} 迫切需要这方面的帮助,我明天要参加 JS 函数考试。以为我已经完成了一切,直到这个大 ole 曲线球。非常感谢任何和所有帮助,谢谢你!

 let people = [{ name: "Nick", studying: "JS", age: "33" }, { name: "Joseph", studying: "JS", age: "137" }, { name: "Jeff", studying: "education", age: "1897374" } ]; function returnArrOfNames(people, string) { let newArr = [] for (let i = 0; i < people.length; i++) { if (people[i].studying === string) { newArr.push(people[i].name) } } return newArr } console.log(returnArrOfNames(people, "JS"));

Before you return newArr you can check it's length and if it's zero return "No matches".在您返回newArr之前,您可以检查它的长度,如果它为零,则返回“无匹配项”。

let people = [{
    name: "Nick",
    studying: "JS",
    age: "33"
  },

  {
    name: "Joseph",
    studying: "JS",
    age: "137"
  },

  {
    name: "Jeff",
    studying: "education",
    age: "1897374"
  }
];

function returnArrOfNames(people, string) {
  let newArr = []
  for (let i = 0; i < people.length; i++) {
    if (people[i].studying === string) {
      newArr.push(people[i].name)
    }
  }

  if (newArr.length === 0) {
    return "No matches";
  }

  return newArr
}

console.log(returnArrOfNames(people, "JS"));

edit: I posted the same answer as someone else sorry.编辑:我发布了与其他人相同的答案,抱歉。

You can just add that if statement outside the for loop.您可以在 for 循环之外添加 if 语句。 Like this:像这样:

let people = [
  {name : "Nick",
  studying : "JS",
  age : "33"},

  {name : "Joseph",
  studying : "JS",
  age : "137"},

  {name : "Jeff",
  studying : "education",
  age : "1897374"}
];

function returnArrOfNames(people, string) {
   let newArr = []
   for (let i = 0; i < people.length; i++) {
      if (people[i].studying === string) {
          newArr.push(people[i].name)
      }
   }
   if (newArr.length == 0){
       return console.log("no matches")
   }

   console.log(newArr);
   return newArr
}


returnArrOfNames(people, "JS")

You can utilize Array prototype's map and filter method to get the result:您可以利用Array prototype's mapfilter方法来获得结果:

 let people = [{ name: "Nick", studying: "JS", age: "33" }, { name: "Joseph", studying: "JS", age: "137" }, { name: "Jeff", studying: "education", age: "1897374" } ]; function returnArrOfNames(people = [], studying) { const NO_MATCHES = 'No Matches' const isEmpty = people.length === 0 if (isEmpty) { return NO_MATCHES } const filterFn = i => i.studying === studying const filteredPeople = people.filter(filterFn) const isEmptyFilteredPeople = filteredPeople.length === 0; if(isEmptyFilteredPeople) return NO_MATCHES const mapFn = i => i.name const mappedPeople = filteredPeople.map(mapFn); return mappedPeople } console.log(returnArrOfNames(people, "JS"));

Good Luck...祝你好运...

the more simple is array.reduce method, and use a ternary condition for return更简单的是 array.reduce 方法,并使用三元条件返回

 const people = [ { name: 'Nick', studying: 'JS', age: '33' } , { name: 'Joseph', studying: 'JS', age: '137' } , { name: 'Jeff', studying: 'education', age: '1897374' } ] function returnArrOfNames(arr, study) { let newArr = arr.reduce((a,c)=> { if (c.studying===study) a.push(c.name) return a }, []) return newArr.length ? newArr : 'no matches' } console.log(' JS ? ->', returnArrOfNames(people, "JS") ) // Array [ "Nick", "Joseph" ] console.log(' xx ? ->', returnArrOfNames(people, "xx") ) // "no matches"
 .as-console-wrapper { max-height: 100% !important; top: 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM