Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中包含特定字母的数组的输出元素_Javascript_Arrays_Function_Syntax_Include - Fatal编程技术网

javascript中包含特定字母的数组的输出元素

javascript中包含特定字母的数组的输出元素,javascript,arrays,function,syntax,include,Javascript,Arrays,Function,Syntax,Include,我是javascript新手,我正在努力使函数语法正确,以便从包含字母“I”的数组中提取元素 let teachers = ["Arrington", "Kincart", "Alberts", "Pickett"] let rooms = [ ["Andy", "Rodolfo", "Lynn", "Talia"], ["Al", "Ross", "Jorge", "Dante"], ["Nick", "Kim", "Jasmine", "Dorothy"], ["Adam

我是javascript新手,我正在努力使函数语法正确,以便从包含字母“I”的数组中提取元素


let teachers = ["Arrington", "Kincart", "Alberts", "Pickett"]

let rooms = [
  ["Andy", "Rodolfo", "Lynn", "Talia"],
  ["Al", "Ross", "Jorge", "Dante"],
  ["Nick", "Kim", "Jasmine", "Dorothy"],
  ["Adam", "Grayson", "Aliyah", "Alexa"]
]

let iInName = (teacher) => {
  let nameWithI = []
  teachers.forEach((withI) => {
    if(teacher.includes('i') {
    nameWithI.push(withI)
  }
})
return nameWithI
}

iInName()


非常感谢您的任何见解。这是我第一次在这里发帖;如果我遗漏了任何信息或忽略了任何礼仪,我很乐意回答

withI
传递给您定义的函数()
teachers
是名称数组,为每个名称调用
withI

if(teacher.includes('i')
应该是
if(withI.includes('i'))
,因为
withI
是教师的名字

你可能还想调查一下


您只需要三个解决方案中的一个,一个用
/*
*/
注释掉,另一个用
/
注释掉。最相似的一个是未注释(已使用).

请向我们展示完整的代码。您发布的内容似乎遗漏了
教师的声明和函数的第一行。您得到的语法错误是什么?非常感谢您的洞察力。没问题。^-^。您需要任何进一步的帮助吗?如果不需要,您可以选择此答案作为回答您的问题的答案离子。
function iInName(teachers) {
  let nameWithI = []
  teachers.forEach((withI) => { // A function or commonly referred to as a callback
    if(withI.includes('i') { // Changed from teachers (of type array)
      nameWithI.push(withI)  // To withI (type string)
    }
  })
  /*
   return teachers.filter((name) => {
      return name.includes('i') // Keeps value if true
    })
  */
  // return teachers.filter(name => name.includes('i'))
  return nameWithI
}
iInName(['joe', 'marie']) // Any array