我正在尝试通过React JS实现动态列表视图,而不使用任何现成的组件。我将需要一个简单的搜索来按用户名查找用户,因此我应该解决这个问题...
例如,我想到并编写了这段代码:
var students = [
{
name: 'David',
age: 12
},
{
name: 'Daniel',
age: 13
},
{
name: 'Alex',
age: 10
}
];
var key = 'Da';
function search(item) {
return item.name.substring(0, key.length) == key;
}
var result = students.filter(search);
在这种情况下,我应该搜索数组,而我没有任何API可以像查询和管理搜索那样处理
。性能和最佳实践对我来说非常重要...
那么,是否使用优化的过滤器和子字符串来实现自定义简单搜索?即使我有一个大型的数组,索引超过1000个!
谢谢
答案 0 :(得分:2)
您可以像这样使用Array.filter和Array.includes:
var students = [ { name: 'David', age: 12 }, { name: 'Daniel', age: 13 }, { name: 'Alex', age: 10 } ];
let searchByFirstName = str => students.filter(({name}) => name.includes(str))
console.log(searchByFirstName('Da'))
console.log(searchByFirstName('ex'))
console.log(searchByFirstName('iel'))
这将检查名称是否在任何“位置”处包括指定的字符串。如果您只关心字符串的开头,则可以使用String.startsWith:
var students = [ { name: 'David', age: 12 }, { name: 'Daniel', age: 13 }, { name: 'Alex', age: 10 } ];
let searchByFirstName = str => students.filter(({name}) => name.startsWith(str))
console.log(searchByFirstName('Da'))
console.log(searchByFirstName('A'))
答案 1 :(得分:1)
如果您只关心性能,只是为了轻笑和咯咯笑声,这是@Akrion答案,可运行近1000条记录。我想您会发现它的运行速度非常快。
const students =[{name:"Candra",age:20},
{name:"Lula",age:6},
{name:"Kamillah",age:8},
{name:"Hally",age:22},
{name:"Kelila",age:5},
{name:"Hyacinthe",age:25},
{name:"Eba",age:28},
{name:"Aimee",age:14},
{name:"Babette",age:30},
{name:"Franky",age:19},
{name:"Francklyn",age:5},
{name:"Fania",age:17},
{name:"Bastian",age:24},
{name:"Lishe",age:18},
{name:"Orlan",age:23},
{name:"Rolando",age:15},
{name:"Corrinne",age:14},
{name:"Rosabel",age:28},
{name:"Cello",age:2},
{name:"Bartholomeo",age:21},
{name:"Jessee",age:25},
{name:"Barry",age:19},
{name:"Loren",age:8},
{name:"Eartha",age:29},
{name:"Kala",age:13},
{name:"Aldrich",age:7},
{name:"Florette",age:25},
{name:"Taffy",age:22},
{name:"Eleni",age:4},
{name:"Nita",age:18},
{name:"Leonid",age:19},
{name:"Serge",age:5},
{name:"Ranee",age:12},
{name:"Glynnis",age:25},
{name:"Rand",age:16},
{name:"Gerty",age:29},
{name:"Arabel",age:12},
{name:"Kelley",age:12},
{name:"Klara",age:13},
{name:"Nonie",age:20},
{name:"Dell",age:15},
{name:"Leslie",age:1},
{name:"Remington",age:2},
{name:"Nikita",age:16},
{name:"Carlynn",age:12},
{name:"Gerome",age:22},
{name:"Omero",age:28},
{name:"Nariko",age:2},
{name:"Gael",age:23},
{name:"Audrye",age:25},
{name:"Hermon",age:21},
{name:"Edwina",age:29},
{name:"Tadeas",age:20},
{name:"Eugenia",age:1},
{name:"Skyler",age:6},
{name:"Lyndell",age:1},
{name:"Tracie",age:28},
{name:"Brianna",age:24},
{name:"Marjy",age:25},
{name:"Waverly",age:19},
{name:"Guthry",age:30},
{name:"Adamo",age:5},
{name:"Gus",age:5},
{name:"Roanna",age:27},
{name:"Penny",age:17},
{name:"Kassey",age:22},
{name:"Gaelan",age:18},
{name:"Dave",age:29},
{name:"Frankie",age:7},
{name:"Alwyn",age:2},
{name:"Quintus",age:14},
{name:"Daren",age:7},
{name:"Marten",age:21},
{name:"Veronike",age:3},
{name:"Matti",age:23},
{name:"Abraham",age:1},
{name:"Bessy",age:20},
{name:"Harwell",age:27},
{name:"Sascha",age:8},
{name:"Allyn",age:19},
{name:"Marabel",age:12},
{name:"Nicholle",age:24},
{name:"Jacinta",age:20},
{name:"Munmro",age:8},
{name:"Emalee",age:27},
{name:"Mollie",age:5},
{name:"Brice",age:12},
{name:"Mareah",age:13},
{name:"Christoph",age:24},
{name:"Rogerio",age:15},
{name:"Bette-ann",age:14},
{name:"Corbet",age:30},
{name:"Carce",age:20},
{name:"Anne-corinne",age:14},
{name:"Candi",age:13},
{name:"Gabbie",age:23},
{name:"Bird",age:21},
{name:"Dunc",age:24},
{name:"Jarret",age:7},
{name:"Saree",age:15},
{name:"Delainey",age:27},
{name:"Tiff",age:15},
{name:"Daryle",age:7},
{name:"Kim",age:13},
{name:"Emmaline",age:15},
{name:"Bentley",age:2},
{name:"Tobiah",age:24},
{name:"Janos",age:23},
{name:"Ruthi",age:29},
{name:"Ulrick",age:13},
{name:"Robby",age:29},
{name:"Leanna",age:15},
{name:"Aimee",age:7},
{name:"Joscelin",age:22},
{name:"Bonnibelle",age:7},
{name:"Hestia",age:29},
{name:"Addia",age:16},
{name:"Magnum",age:14},
{name:"Ashley",age:24},
{name:"Inessa",age:11},
{name:"Everard",age:29},
{name:"James",age:17},
{name:"Ferdinand",age:29},
{name:"Sheela",age:29},
{name:"Jobye",age:13},
{name:"Fitzgerald",age:18},
{name:"Wilburt",age:22},
{name:"Ronna",age:16},
{name:"Beck",age:17},
{name:"Carlyn",age:29},
{name:"Burty",age:15},
{name:"Daryn",age:22},
{name:"Marga",age:4},
{name:"Carlin",age:28},
{name:"Leonard",age:15},
{name:"Gilberte",age:30},
{name:"Anderson",age:20},
{name:"Deana",age:19},
{name:"Beitris",age:27},
{name:"Vitia",age:23},
{name:"Ami",age:20},
{name:"Jo",age:13},
{name:"Brent",age:7},
{name:"Louisette",age:24},
{name:"Winn",age:20},
{name:"Livvie",age:15},
{name:"Edithe",age:20},
{name:"Edlin",age:17},
{name:"Chet",age:7},
{name:"Johnathon",age:24},
{name:"Cicily",age:18},
{name:"Loria",age:23},
{name:"Jaye",age:21},
{name:"Constantine",age:8},
{name:"Hillie",age:17},
{name:"Osmond",age:5},
{name:"Marnie",age:4},
{name:"Roberta",age:8},
{name:"Corinna",age:1},
{name:"Findley",age:3},
{name:"Lorelei",age:10},
{name:"Ella",age:6},
{name:"Maxie",age:23},
{name:"Manolo",age:26},
{name:"Elyse",age:22},
{name:"Cobby",age:9},
{name:"Bryn",age:11},
{name:"Julia",age:17},
{name:"Anabella",age:3},
{name:"Dona",age:4},
{name:"Zenia",age:4},
{name:"Cyndi",age:7},
{name:"Ty",age:17},
{name:"Crystie",age:10},
{name:"Ed",age:19},
{name:"Lissie",age:14},
{name:"Syman",age:11},
{name:"Hussein",age:14},
{name:"Hyacintha",age:27},
{name:"Faydra",age:23},
{name:"Hamlen",age:8},
{name:"Lethia",age:21},
{name:"Emery",age:23},
{name:"Chaunce",age:5},
{name:"Cosette",age:11},
{name:"Meade",age:19},
{name:"Frannie",age:1},
{name:"Roby",age:25},
{name:"Leigh",age:11},
{name:"Justus",age:30},
{name:"Beret",age:19},
{name:"Nelli",age:14},
{name:"Chico",age:10},
{name:"Aloise",age:29},
{name:"Noah",age:24},
{name:"Heriberto",age:8},
{name:"Matilde",age:7},
{name:"Anetta",age:13},
{name:"Terrie",age:18},
{name:"Clementius",age:1},
{name:"Casie",age:19},
{name:"Lydie",age:2},
{name:"Nickie",age:4},
{name:"Ashil",age:8},
{name:"Aloisia",age:4},
{name:"Shannon",age:13},
{name:"Valentin",age:20},
{name:"Gunther",age:14},
{name:"Devan",age:7},
{name:"Jayme",age:23},
{name:"Garv",age:28},
{name:"Thain",age:15},
{name:"Carin",age:10},
{name:"Diandra",age:25},
{name:"Ambros",age:5},
{name:"Mace",age:5},
{name:"Thalia",age:14},
{name:"Timoteo",age:12},
{name:"Rickie",age:26},
{name:"Izabel",age:3},
{name:"Frazier",age:1},
{name:"Osborn",age:5},
{name:"Traci",age:11},
{name:"Scott",age:26},
{name:"Gun",age:21},
{name:"Kevin",age:13},
{name:"Stacie",age:10},
{name:"Felix",age:28},
{name:"Adele",age:9},
{name:"Deck",age:9},
{name:"Sadie",age:3},
{name:"Berty",age:1},
{name:"Brunhilde",age:15},
{name:"Amelia",age:4},
{name:"Calley",age:28},
{name:"Sherie",age:14},
{name:"Jere",age:14},
{name:"Tally",age:4},
{name:"Bridie",age:12},
{name:"Maible",age:25},
{name:"Annissa",age:3},
{name:"Byrom",age:10},
{name:"Annabal",age:10},
{name:"Ahmad",age:28},
{name:"Christian",age:15},
{name:"Fabian",age:14},
{name:"Yehudit",age:10},
{name:"Fanny",age:7},
{name:"Peirce",age:7},
{name:"Reidar",age:14},
{name:"Davie",age:18},
{name:"Renee",age:29},
{name:"Celie",age:8},
{name:"Cosimo",age:5},
{name:"Kissee",age:12},
{name:"Chicky",age:20},
{name:"Antonella",age:8},
{name:"Dominique",age:20},
{name:"Levey",age:23},
{name:"Emlynn",age:22},
{name:"Hercules",age:20},
{name:"Gratiana",age:5},
{name:"Gill",age:15},
{name:"Silvana",age:27},
{name:"Ursula",age:16},
{name:"Leoline",age:15},
{name:"Micky",age:23},
{name:"Mahala",age:7},
{name:"Candra",age:4},
{name:"Astrix",age:21},
{name:"Hamlin",age:16},
{name:"Marissa",age:24},
{name:"Wendeline",age:4},
{name:"Dawna",age:16},
{name:"Roddy",age:27},
{name:"Thurstan",age:17},
{name:"Rooney",age:28},
{name:"Marissa",age:7},
{name:"Bethina",age:24},
{name:"Bartholomew",age:20},
{name:"Maury",age:20},
{name:"Clive",age:23},
{name:"Frasco",age:12},
{name:"Bond",age:7},
{name:"Sampson",age:27},
{name:"Zabrina",age:20},
{name:"Erhart",age:21},
{name:"Emily",age:29},
{name:"Laryssa",age:5},
{name:"Sophia",age:13},
{name:"Lavinie",age:16},
{name:"Martguerita",age:7},
{name:"Eldin",age:15},
{name:"Tish",age:12},
{name:"Abigael",age:11},
{name:"Prentiss",age:17},
{name:"Taddeo",age:3},
{name:"Leandra",age:15},
{name:"Carmelle",age:27},
{name:"Adey",age:23},
{name:"Spenser",age:27},
{name:"Wallis",age:28},
{name:"Worth",age:15},
{name:"Pancho",age:8},
{name:"Briggs",age:13},
{name:"Marta",age:18},
{name:"Agnella",age:11},
{name:"Jeanne",age:29},
{name:"Lynne",age:13},
{name:"Selie",age:1},
{name:"Barnaby",age:1},
{name:"Delinda",age:22},
{name:"Korie",age:9},
{name:"Robinett",age:5},
{name:"Kamila",age:3},
{name:"Gottfried",age:25},
{name:"Donni",age:4},
{name:"Andriette",age:1},
{name:"Tybie",age:15},
{name:"Kailey",age:26},
{name:"Turner",age:12},
{name:"Roman",age:19},
{name:"Benedikt",age:17},
{name:"Yolane",age:3},
{name:"Humfrid",age:25},
{name:"Adel",age:26},
{name:"Jeremy",age:1},
{name:"Perri",age:24},
{name:"Farrand",age:18},
{name:"Dasi",age:17},
{name:"Rob",age:2},
{name:"Barbey",age:13},
{name:"Gabriele",age:7},
{name:"Lena",age:7},
{name:"Kettie",age:23},
{name:"Catharina",age:5},
{name:"Sherwood",age:8},
{name:"Myrtie",age:11},
{name:"Evaleen",age:5},
{name:"Jaine",age:9},
{name:"Minette",age:11},
{name:"Idaline",age:15},
{name:"Maurie",age:13},
{name:"Karim",age:12},
{name:"Anjanette",age:18},
{name:"Rockey",age:27},
{name:"Correy",age:14},
{name:"Esma",age:23},
{name:"Lurette",age:29},
{name:"Wheeler",age:23},
{name:"Marlon",age:18},
{name:"Douglas",age:18},
{name:"Kali",age:9},
{name:"Deloria",age:2},
{name:"Darla",age:6},
{name:"Elie",age:29},
{name:"Magdalene",age:21},
{name:"Ealasaid",age:12},
{name:"Toma",age:17},
{name:"Brit",age:1},
{name:"Lynne",age:9},
{name:"Jany",age:19},
{name:"Gibb",age:15},
{name:"Sadella",age:9},
{name:"Rick",age:16},
{name:"Reinold",age:18},
{name:"Dorey",age:7},
{name:"Asa",age:8},
{name:"Annemarie",age:28},
{name:"Dillie",age:21},
{name:"Alameda",age:1},
{name:"Letti",age:27},
{name:"Durante",age:1},
{name:"Tallie",age:4},
{name:"Kristal",age:3},
{name:"Bee",age:13},
{name:"Haleigh",age:4},
{name:"Anton",age:4},
{name:"Findlay",age:23},
{name:"Joyann",age:26},
{name:"Brianna",age:27},
{name:"Emyle",age:28},
{name:"Mycah",age:6},
{name:"Lorry",age:24},
{name:"Candie",age:12},
{name:"Raff",age:11},
{name:"Jsandye",age:1},
{name:"Miof mela",age:15},
{name:"Eddy",age:5},
{name:"Torrence",age:27},
{name:"Eustacia",age:18},
{name:"Brynna",age:12},
{name:"Dougie",age:12},
{name:"Aindrea",age:14},
{name:"Karalee",age:21},
{name:"Willyt",age:28},
{name:"Reagen",age:16},
{name:"Van",age:17},
{name:"Tobit",age:14},
{name:"Mariellen",age:8},
{name:"Karia",age:8},
{name:"Taddeo",age:27},
{name:"Mar",age:27},
{name:"Bab",age:24},
{name:"Cele",age:27},
{name:"Hazel",age:24},
{name:"Clevey",age:5},
{name:"Merry",age:16},
{name:"Caz",age:14},
{name:"Aloin",age:1},
{name:"Daune",age:28},
{name:"Olivero",age:10},
{name:"Haze",age:30},
{name:"Lacie",age:7},
{name:"Elnora",age:29},
{name:"Betteanne",age:5},
{name:"Isacco",age:11},
{name:"Domini",age:16},
{name:"Doralia",age:15},
{name:"Lyda",age:29},
{name:"Florrie",age:20},
{name:"Husein",age:14},
{name:"Gaspard",age:16},
{name:"Adrienne",age:14},
{name:"Neala",age:2},
{name:"Timmie",age:28},
{name:"Sibilla",age:21},
{name:"Isa",age:6},
{name:"Filbert",age:11},
{name:"Horten",age:27},
{name:"Craig",age:19},
{name:"Ware",age:16},
{name:"Romeo",age:7},
{name:"Ettie",age:10},
{name:"Jada",age:3},
{name:"Annissa",age:12},
{name:"Coretta",age:29},
{name:"Laure",age:4},
{name:"Fergus",age:10},
{name:"Rog",age:22},
{name:"Cinnamon",age:28},
{name:"Seka",age:23},
{name:"Florian",age:26},
{name:"Jeanelle",age:22},
{name:"Benni",age:4},
{name:"Hill",age:25},
{name:"Meyer",age:8},
{name:"Ruprecht",age:3},
{name:"Duke",age:24},
{name:"Martelle",age:21},
{name:"Inessa",age:9},
{name:"Thorvald",age:30},
{name:"Lilith",age:25},
{name:"Rogerio",age:3},
{name:"Elisha",age:11},
{name:"Darcee",age:23},
{name:"Merrill",age:13},
{name:"Obie",age:27},
{name:"Fianna",age:6},
{name:"Ulric",age:26},
{name:"Miranda",age:12},
{name:"Granger",age:14},
{name:"Brandon",age:23},
{name:"Jada",age:1},
{name:"Sayre",age:25},
{name:"Julius",age:30},
{name:"Thomasine",age:5},
{name:"Celinka",age:26},
{name:"Rhetta",age:24},
{name:"Sosanna",age:8},
{name:"Dilly",age:7},
{name:"Noellyn",age:1},
{name:"Bernard",age:22},
{name:"Carree",age:27},
{name:"Antonius",age:16},
{name:"Magdaia",age:4},
{name:"Clarabelle",age:15},
{name:"Goldina",age:30},
{name:"Hazel",age:10},
{name:"Tarrah",age:10},
{name:"Sosanna",age:21},
{name:"Bernarr",age:11},
{name:"Jacobo",age:8},
{name:"Whittaker",age:18},
{name:"Jazmin",age:17},
{name:"Giustino",age:28},
{name:"Gasparo",age:4},
{name:"Margarethe",age:25},
{name:"Stanislaus",age:22},
{name:"Marlin",age:28},
{name:"Sheffield",age:18},
{name:"Ulrika",age:26},
{name:"Lilith",age:17},
{name:"Jacky",age:27},
{name:"Calv",age:28},
{name:"Jerrie",age:11},
{name:"Corissa",age:5},
{name:"Kamilah",age:28},
{name:"Karrie",age:20},
{name:"Heidie",age:30},
{name:"Sheppard",age:9},
{name:"Gretta",age:7},
{name:"Wiatt",age:14},
{name:"Mira",age:18},
{name:"Sinclair",age:7},
{name:"Philippe",age:9},
{name:"Augusto",age:26},
{name:"Brandea",age:13},
{name:"Rudolph",age:11},
{name:"Windy",age:3},
{name:"Arie",age:18},
{name:"Orelle",age:20},
{name:"Dara",age:30},
{name:"Diannne",age:9},
{name:"Cozmo",age:16},
{name:"Leigh",age:23},
{name:"Dareen",age:20},
{name:"Tommie",age:24},
{name:"Vera",age:21},
{name:"Ferrel",age:23},
{name:"Jaimie",age:12},
{name:"Tiler",age:6},
{name:"Natala",age:30},
{name:"Mariele",age:16},
{name:"Lotty",age:25},
{name:"Lauralee",age:22},
{name:"Livvyy",age:23},
{name:"Algernon",age:8},
{name:"Nelson",age:12},
{name:"Carlota",age:16},
{name:"Sigmund",age:30},
{name:"Grove",age:11},
{name:"Gianna",age:20},
{name:"Sofie",age:12},
{name:"Beatriz",age:29},
{name:"Aksel",age:20},
{name:"Rahal",age:26},
{name:"Tillie",age:16},
{name:"Carlen",age:11},
{name:"Lindsy",age:19},
{name:"Ilyssa",age:20},
{name:"Marlin",age:11},
{name:"Felicdad",age:11},
{name:"Quintana",age:30},
{name:"Riane",age:5},
{name:"Jojo",age:24},
{name:"Tamera",age:26},
{name:"Taddeusz",age:4},
{name:"Elston",age:8},
{name:"Kylen",age:28},
{name:"Vinnie",age:27},
{name:"Thurstan",age:17},
{name:"Jillana",age:13},
{name:"Chantal",age:26},
{name:"Morten",age:6},
{name:"Hoebart",age:8},
{name:"Giulio",age:19},
{name:"Tobin",age:16},
{name:"Lamar",age:13},
{name:"Tam",age:14},
{name:"Boigie",age:28},
{name:"Deborah",age:21},
{name:"Loydie",age:11},
{name:"Cam",age:22},
{name:"Danya",age:26},
{name:"Saxon",age:12},
{name:"Corbie",age:8},
{name:"Mal",age:18},
{name:"Meredeth",age:2},
{name:"Moll",age:19},
{name:"Josefina",age:21},
{name:"Jarrid",age:10},
{name:"Rafael",age:8},
{name:"Dorice",age:11},
{name:"Jermayne",age:24},
{name:"Sancho",age:3},
{name:"Eduard",age:6},
{name:"Oliver",age:24},
{name:"Cullin",age:15},
{name:"Cobb",age:15},
{name:"Henrie",age:12},
{name:"Willy",age:5},
{name:"Moise",age:21},
{name:"Selig",age:2},
{name:"Jerrilyn",age:17},
{name:"Durand",age:25},
{name:"Jacqui",age:15},
{name:"Kristin",age:1},
{name:"Cordie",age:28},
{name:"Tomaso",age:10},
{name:"Wayland",age:1},
{name:"Armando",age:8},
{name:"Corey",age:19},
{name:"Arch",age:2},
{name:"Kimbell",age:22},
{name:"Allyn",age:28},
{name:"Aeriela",age:20},
{name:"Ivor",age:23},
{name:"Felipa",age:21},
{name:"Remington",age:20},
{name:"Meredith",age:23},
{name:"Beret",age:9},
{name:"Andee",age:16},
{name:"Augustine",age:3},
{name:"Birgit",age:8},
{name:"Malcolm",age:6},
{name:"Farand",age:20},
{name:"Ernesto",age:3},
{name:"Marci",age:26},
{name:"Celinda",age:19},
{name:"Kristi",age:26},
{name:"Patrizius",age:17},
{name:"Dorolisa",age:26},
{name:"Reggie",age:2},
{name:"Terry",age:22},
{name:"Chandal",age:8},
{name:"Valle",age:20},
{name:"Artemas",age:22},
{name:"Doreen",age:8},
{name:"Bertina",age:3},
{name:"Bruce",age:9},
{name:"Efren",age:17},
{name:"Kelsy",age:28},
{name:"Theresina",age:24},
{name:"Jessy",age:4},
{name:"Layney",age:28},
{name:"Shepperd",age:25},
{name:"Dion",age:27},
{name:"Benji",age:2},
{name:"Barclay",age:7},
{name:"Bennett",age:18},
{name:"Debora",age:17},
{name:"Mikol",age:16},
{name:"Benni",age:21},
{name:"Nicolai",age:1},
{name:"Nicol",age:21},
{name:"Ramsay",age:10},
{name:"Glynis",age:3},
{name:"Gawain",age:7},
{name:"Fleurette",age:26},
{name:"Nichole",age:17},
{name:"Barrett",age:29},
{name:"Pete",age:28},
{name:"Ruthann",age:20},
{name:"Charlotte",age:22},
{name:"Forrester",age:4},
{name:"Dorolisa",age:9},
{name:"Erhard",age:16},
{name:"Vida",age:26},
{name:"Vidovic",age:3},
{name:"Kipp",age:1},
{name:"Samara",age:29},
{name:"Yank",age:2},
{name:"Aliza",age:20},
{name:"Selestina",age:7},
{name:"Lou",age:12},
{name:"Eldin",age:7},
{name:"Nelly",age:9},
{name:"Datha",age:7},
{name:"Kylen",age:24},
{name:"Albert",age:19},
{name:"Anderea",age:22},
{name:"Meredith",age:14},
{name:"Valdemar",age:18},
{name:"Hymie",age:29},
{name:"Darcy",age:29},
{name:"Philippine",age:12},
{name:"Stu",age:27},
{name:"Reube",age:26},
{name:"Kathlin",age:9},
{name:"Ancell",age:17},
{name:"Brucie",age:3},
{name:"Jorry",age:25},
{name:"Marita",age:27},
{name:"Nananne",age:14},
{name:"Florian",age:11},
{name:"Lindon",age:25},
{name:"Stepha",age:26},
{name:"Terza",age:7},
{name:"Brander",age:15},
{name:"Elnora",age:21},
{name:"Delphine",age:19},
{name:"Quinton",age:3},
{name:"Geordie",age:8},
{name:"Manya",age:15},
{name:"Matty",age:26},
{name:"Anabelle",age:10},
{name:"Abbi",age:23},
{name:"Lyndel",age:10},
{name:"Sean",age:30},
{name:"Shannan",age:19},
{name:"Iorgo",age:8},
{name:"Damien",age:25},
{name:"Sampson",age:7},
{name:"Marjie",age:23},
{name:"Myrlene",age:4},
{name:"Christa",age:22},
{name:"Lenora",age:27},
{name:"Haleigh",age:25},
{name:"Herold",age:18},
{name:"Kurt",age:13},
{name:"Christian",age:28},
{name:"Elladine",age:1},
{name:"Zeb",age:2},
{name:"Lamar",age:6},
{name:"Gusty",age:27},
{name:"Giffie",age:23},
{name:"Aubree",age:1},
{name:"Ertha",age:23},
{name:"Giulio",age:20},
{name:"Maritsa",age:29},
{name:"Ahmed",age:20},
{name:"Marylynne",age:19},
{name:"Kellen",age:6},
{name:"Dayna",age:8},
{name:"Dietrich",age:24},
{name:"Zacherie",age:11},
{name:"Galven",age:7},
{name:"Paco",age:16},
{name:"Kattie",age:22},
{name:"Kris",age:3},
{name:"Corbie",age:3},
{name:"Phoebe",age:15},
{name:"Lacie",age:12},
{name:"Leyla",age:16},
{name:"Inessa",age:25},
{name:"Dareen",age:11},
{name:"Massimiliano",age:15},
{name:"Brok",age:20},
{name:"Alexine",age:25},
{name:"Devy",age:3},
{name:"Eddie",age:2},
{name:"Floyd",age:26},
{name:"Kylen",age:17},
{name:"Farlay",age:18},
{name:"Cori",age:18},
{name:"Berk",age:17},
{name:"Caleb",age:2},
{name:"Valaree",age:14},
{name:"Julina",age:23},
{name:"Cletus",age:16},
{name:"Bale",age:26},
{name:"Barton",age:2},
{name:"Gwenneth",age:7},
{name:"Joleen",age:20},
{name:"Charmion",age:6},
{name:"Roxana",age:5},
{name:"Connor",age:9},
{name:"Read",age:3},
{name:"Wandie",age:3},
{name:"Phillipp",age:13},
{name:"Brennan",age:10},
{name:"Emilio",age:13},
{name:"Bonnie",age:27},
{name:"Harlen",age:9},
{name:"Prescott",age:21},
{name:"Kandy",age:9},
{name:"Preston",age:15},
{name:"Derward",age:23},
{name:"Terrie",age:9},
{name:"Juliane",age:29},
{name:"Galina",age:20},
{name:"Kelcy",age:10},
{name:"Binnie",age:22},
{name:"Lin",age:1},
{name:"Quent",age:30},
{name:"Teresina",age:30},
{name:"Nial",age:4},
{name:"Marketa",age:27},
{name:"Willow",age:13},
{name:"Brigham",age:19},
{name:"Isidora",age:5},
{name:"Sherwood",age:6},
{name:"Nanete",age:29},
{name:"Stacee",age:2},
{name:"Corena",age:14},
{name:"Emmye",age:18},
{name:"Gino",age:10},
{name:"Isabella",age:19},
{name:"Barnebas",age:21},
{name:"Atlante",age:26},
{name:"Ardra",age:18},
{name:"Indira",age:12},
{name:"Ola",age:7},
{name:"Stefa",age:14},
{name:"Raymund",age:23},
{name:"Kassey",age:16},
{name:"Brandon",age:9},
{name:"Kimbell",age:21},
{name:"Grethel",age:21},
{name:"Michale",age:22},
{name:"Clarance",age:6},
{name:"Lazarus",age:19},
{name:"Pebrook",age:28},
{name:"Delainey",age:23},
{name:"Morty",age:17},
{name:"Lil",age:28},
{name:"Hazlett",age:14},
{name:"Ryan",age:4},
{name:"Noellyn",age:26},
{name:"Dolley",age:11},
{name:"Shanie",age:27},
{name:"Kellsie",age:1},
{name:"Karl",age:17},
{name:"Dimitri",age:28},
{name:"Sianna",age:22},
{name:"Constancy",age:24},
{name:"Tiffie",age:14},
{name:"Levey",age:6},
{name:"Sybilla",age:14},
{name:"Layla",age:22},
{name:"Bamby",age:28},
{name:"Gun",age:26},
{name:"Emmerich",age:13},
{name:"Orran",age:12},
{name:"Janot",age:18},
{name:"Dorita",age:24},
{name:"Bran",age:10},
{name:"Miriam",age:1},
{name:"Desdemona",age:10},
{name:"Matelda",age:12},
{name:"Minnnie",age:25},
{name:"Julian",age:27},
{name:"Hyman",age:11},
{name:"Selia",age:11},
{name:"Joleen",age:3},
{name:"Agosto",age:25},
{name:"Allison",age:2},
{name:"Lorilee",age:21},
{name:"Stavros",age:24},
{name:"Karlotta",age:19},
{name:"Reg",age:20},
{name:"Ulises",age:6},
{name:"Jenelle",age:8},
{name:"Lorry",age:22},
{name:"Valery",age:19},
{name:"Curcio",age:24},
{name:"Winnifred",age:5},
{name:"Liza",age:10},
{name:"Arabela",age:11},
{name:"Ann-marie",age:8},
{name:"Royal",age:22},
{name:"Ed",age:8},
{name:"Devi",age:10},
{name:"Sabra",age:1},
{name:"Taber",age:7},
{name:"Harlan",age:14},
{name:"Bartram",age:21},
{name:"Antons",age:17},
{name:"Bax",age:24},
{name:"Trevor",age:25},
{name:"Vivie",age:6},
{name:"Elnar",age:15},
{name:"Paola",age:29},
{name:"Preston",age:26},
{name:"Kore",age:20},
{name:"Kenny",age:30},
{name:"Kelcey",age:10},
{name:"Kendell",age:10},
{name:"Susanne",age:29},
{name:"Edik",age:14},
{name:"Donovan",age:18},
{name:"Jobey",age:22},
{name:"Shari",age:8},
{name:"Janel",age:15},
{name:"Banky",age:18},
{name:"Nanine",age:12},
{name:"Bel",age:23},
{name:"Matilde",age:1},
{name:"Freddy",age:10},
{name:"Auroora",age:22},
{name:"Burch",age:27},
{name:"Merline",age:22},
{name:"Elberta",age:24},
{name:"Abagail",age:3},
{name:"Viviene",age:16},
{name:"Selia",age:6},
{name:"Robinette",age:8},
{name:"Emmanuel",age:11},
{name:"Dyana",age:19},
{name:"Valencia",age:13},
{name:"Orella",age:10},
{name:"Cull",age:10},
{name:"Mersey",age:28},
{name:"Belinda",age:8},
{name:"Antonio",age:10},
{name:"Cody",age:8},
{name:"Briana",age:10},
{name:"Odette",age:20},
{name:"Virgil",age:14},
{name:"Dolley",age:16},
{name:"Sally",age:20},
{name:"Tina",age:15},
{name:"Warren",age:7},
{name:"Sibilla",age:1},
{name:"Neall",age:18},
{name:"Trenna",age:27},
{name:"Vittorio",age:3},
{name:"Daphne",age:6},
{name:"Skipper",age:17},
{name:"Adelind",age:26},
{name:"Adrianna",age:11},
{name:"Stirling",age:22},
{name:"Tremayne",age:15},
{name:"Theodor",age:2},
{name:"Preston",age:11},
{name:"Rickey",age:27},
{name:"Bourke",age:14},
{name:"Adena",age:1},
{name:"Trever",age:24},
{name:"Gearard",age:9},
{name:"Muffin",age:7},
{name:"Murry",age:22},
{name:"Hephzibah",age:8},
{name:"Dud",age:15},
{name:"Kakalina",age:26},
{name:"Morey",age:19},
{name:"Sayres",age:6},
{name:"Neysa",age:28},
{name:"Isabelle",age:20},
{name:"Lorenza",age:3},
{name:"Philly",age:21},
{name:"Silvie",age:19},
{name:"Werner",age:1},
{name:"Tiebold",age:21},
{name:"Nels",age:5},
{name:"Clemens",age:2},
{name:"Hewe",age:10},
{name:"Alisun",age:8},
{name:"Ewan",age:18},
{name:"Linnet",age:18},
{name:"Halimeda",age:24},
{name:"Jewell",age:30},
{name:"Mac",age:11},
{name:"Estell",age:15},
{name:"Orren",age:5},
{name:"Evangelin",age:30},
{name:"Mahmoud",age:4},
{name:"Codi",age:4},
{name:"Frances",age:21},
{name:"Elston",age:11},
{name:"Isidoro",age:3},
{name:"Gill",age:26},
{name:"Merrie",age:27},
{name:"Gill",age:1},
{name:"Cos",age:26},
{name:"Patricia",age:13},
{name:"Shaun",age:26},
{name:"Georgianne",age:7},
{name:"Walliw",age:15},
{name:"Cherri",age:29},
{name:"Nicola",age:3},
{name:"Benoite",age:29},
{name:"Daven",age:24},
{name:"Dulcine",age:13},
{name:"Vinny",age:21},
{name:"Land",age:7},
{name:"Tresa",age:14},
{name:"Cherish",age:30},
{name:"Elbertina",age:5},
{name:"Louisa",age:24},
{name:"Aleta",age:24},
{name:"Sib",age:26},
{name:"Merla",age:24},
{name:"Lemmie",age:14},
{name:"Cherrita",age:19},
{name:"Tirrell",age:26},
{name:"Calhoun",age:4},
{name:"Farris",age:14},
{name:"Isabelita",age:25},
{name:"Jeffry",age:2},
{name:"Vania",age:23},
{name:"Ashia",age:15},
{name:"Farris",age:30},
{name:"Evita",age:19},
{name:"Tina",age:13},
{name:"Candra",age:26},
{name:"Orlan",age:4},
{name:"Modestia",age:14},
{name:"Darla",age:20},
{name:"Jervis",age:2}];
class Thingy extends React.Component {
state={term: '', results:[]};
onChange = (e) => {
const term = e.target.value;
this.setState({
term,
results: students.filter(({name}) => name.startsWith(term))
});
}
render() {
const {term, results} = this.state;
return (
<div>
<label>Name (case sensitive)</label>
<input value={term} onChange={this.onChange}/>
<h3>Results</h3>
<hr/>
{results.map((r,i) => (
<p key={i}>
{`${r.name}, ${r.age}`}
</p>
))}
</div>
);
}
}
// Render it
ReactDOM.render(
<Thingy />,
document.getElementById("root")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
答案 2 :(得分:-1)
这是一个带正则表达式的解决方案,该解决方案返回与您的搜索词匹配的所有名称。使用正则表达式时,天空是极限。
这将返回两个学生对象(David和Daniel)
function getName(studentList, name){
let regex = new RegExp(name);
let result = [];
studentList.forEach(function (student){
if (student.name.search(regex) > -1){
result.push(student);
}
})
return result;
}
const students = [ { name: 'David', age: 12 }, { name: 'Daniel', age: 13 }, { name: 'Alex', age: 10 } ];
console.log(getName(students, 'Da'));