admin管理员组文章数量:1386660
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
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 i.e:
const filterZach = users
.filter(user => user.firstName.startsWith("Zach"))
.map(user => {
return `<p>${user.firstName} ${user.lastName}</p>`;
}).join(""); // get rid of as
Desired Result: 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: i.e:
// helper
function filteredNames(arr, str) {
return arr.filter(i => i.startsWith(str));
}
I get an error saying: i.startsWith is not a function
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 acplish 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. Reduce perhaps?
here's a code sandbox
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
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 i.e:
const filterZach = users
.filter(user => user.firstName.startsWith("Zach"))
.map(user => {
return `<p>${user.firstName} ${user.lastName}</p>`;
}).join(""); // get rid of as
Desired Result: 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: i.e:
// helper
function filteredNames(arr, str) {
return arr.filter(i => i.startsWith(str));
}
I get an error saying: i.startsWith is not a function
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 acplish 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. Reduce perhaps?
here's a code sandbox
Share Improve this question edited Jan 20, 2019 at 0:58 Null isTrue asked Jan 20, 2019 at 0:55 Null isTrueNull isTrue 1,9368 gold badges31 silver badges50 bronze badges1 Answer
Reset to default 5Inside 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:
// 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
function filteredNames(arr, str, prop) {
return arr.filter(i => i[prop].startsWith(str));
}
本文标签: ecmascript 6JavaScript HelperstartsWith( ) is not a functionStack Overflow
版权声明:本文标题:ecmascript 6 - JavaScript Helper | startsWith( ) is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744548909a2612065.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论