简体   繁体   English

JavaScript助手| startsWith()不是函数

[英]JavaScript Helper | startsWith( ) is not a function

I need to write a helper function that loops through an array of objects & filters all names that meet established criteria. 我需要编写一个帮助程序函数 ,该函数遍历对象数组并过滤所有符合既定条件的名称。 In my case all names that begin with Zach 就我而言,所有以Zach开头的名字

Array example: 数组示例:

const users = [
  {
    firstName: "Zach",
    id: 1,
    lastName: "Volsk"
  },
  {
    firstName: "Surly",
    id: 2,
    lastName: "Furious"
  },

  {
    firstName: "Zach",
    id: 3,
    lastName: "Tee"
  },
  {
    firstName: "Eve",
    id: 4,
    lastName: "Zelda"
  },

  {
    firstName: "Zachary",
    id: 5,
    lastName: "Toys"
  }
];

I'm able to retrieve the desired result applying filter and map directly ie: 我能够使用过滤器直接检索所需的结果并映射,即:

const filterZach = users
  .filter(user => user.firstName.startsWith("Zach"))
  .map(user => {
    return `<p>${user.firstName} ${user.lastName}</p>`;
  }).join(""); // get rid of comas

Desired Result: Zach Volsk, Zach Tee, Zachary Toys Zach Volsk, Zach Tee, Zachary Toys结果: Zach Volsk, Zach Tee, Zachary Toys

However, when I tried to abstract that functionality into a helper method so that I can use it elsewhere in my code: ie: 但是,当我尝试将该功能抽象为一个辅助方法时,可以在代码的其他地方使用它:即:

// helper
function filteredNames(arr, str) {
  return arr.filter(i => i.startsWith(str));
}

I get an error saying: i.startsWith is not a function 我收到一条错误消息: i.startsWith不是函数

const filterZach = filteredNames(users, "Zach") // i.startsWith is not a function
const renderZach = filterZach.map(user => {
    return `<p>${user.firstName} ${user.lastName}</p>`;
  }).join("");

Question: Could you please help understand the reason for this issue showing me an alternative to accomplish my goal using a helper function? 问题:能否请您帮助理解此问题的原因,向我显示使用助手功能实现目标的替代方法?

I'm not married to the startsWith( ) method and I'd love to learn other ways to go about this. 我还没有嫁给startsWith( )方法,我想学习其他解决方法。 Reduce perhaps? 减少也许?

here's a code sandbox 这是一个代码沙箱

Inside filteredNames , with arr.filter(i , i is an object , not a string, and objects don't have a startsWith method. Reference the object's firstName instead, and call startsWith on it: filteredNames ,使用arr.filter(ii是一个对象 ,不是字符串,并且对象没有startsWith方法。而是引用该对象的firstName ,然后对其调用startsWith

// helper
function filteredNames(arr, str) {
  return arr.filter(i => i.firstName.startsWith(str));
}

 const users = [ { firstName: "Zach", id: 1, lastName: "Volsk" }, { firstName: "Surly", id: 2, lastName: "Furious" }, { firstName: "Zach", id: 4, lastName: "Tee" }, { firstName: "Eve", id: 5, lastName: "Zelda" }, { firstName: "Zachary", id: 6, lastName: "Toys" } ]; // helper function filteredNames(arr, str) { return arr.filter(i => i.firstName.startsWith(str)); } const filterZach = filteredNames(users, "Zach") // i.startsWith is not a function const renderZach = filterZach.map(user => { return `<p>${user.firstName} ${user.lastName}</p>`; }).join(""); document.body.innerHTML += renderZach; 

For a dynamic property name to call startsWith on, pass that property name to filteredNames , eg 要使动态属性名称调用startsWith on,请将该属性名称传递给filteredNames ,例如

function filteredNames(arr, str, prop) {
  return arr.filter(i => i[prop].startsWith(str));
}

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

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