Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  27] [ 2]  / answers: 1 / hits: 26784  / 7 Years ago, wed, may 10, 2017, 12:00:00

I have a function that is giving me some trouble. The code below returns the error message Cannot read property 'value' of undefined. The function should just search through the values in the accountlist and return the one that starts with the submitted string. In the example, submitting 000555 should return 0.





var accountlist = [{
value: 000555 - TEST ACCOUNT NAME1,
data: 184
}, {
value: 006666 - TEST ACCOUNT NAME2,
data: 450
}, {
value: 007777 - TEST ACCOUNT NAME2,
data: 451
}];

function startswith(inputlist, searchkey, inputstring) {
var searchlength = inputstring.length;
console.log(starting search);

for (var il = 0; il < inputlist.length; il++) {
if (inputlist[il].window[searchkey].substring(0, (searchlength - 1)) == inputstring) {
console.log(FOUND IT + il + + inputstring);
return il
}
}
}

startswith(accountlist, value,000555);




More From » javascript

 Answers
51

You could use the find function:





var accountlist = [{
value: 000555 - TEST ACCOUNT NAME1,
data: 184
}, {
value: 006666 - TEST ACCOUNT NAME2,
data: 450
}, {
value: 007777 - TEST ACCOUNT NAME2,
data: 451
}];
var searchString = '000555';
var result = accountlist.findIndex((account) => { return account.value.startsWith(searchString);}, searchString)
console.log(result)




[#57829] Monday, May 8, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dylondaytond

Total Points: 92
Total Questions: 88
Total Answers: 96

Location: China
Member since Fri, Jan 15, 2021
3 Years ago
dylondaytond questions
Tue, Jun 22, 21, 00:00, 3 Years ago
Thu, May 7, 20, 00:00, 4 Years ago
;