I have an array with objects, and each object containing a property called name.
Now i want to have a second array containing the first letter of each name property from the first array. This first letter should be saved in a property called header if it doesn't exist yet.
I only want to have a letter to appear once, as this script is intended to be used for a list of alphabetic headers, each containing a list of names starting with the same letter.
I have created a function that uses the build-in JavaScript "filter" method, which is supposed to check if a value with this first letter already exists. But for some reason it doesn't work. The filter always returns an empty array, no matter what letter i provide.
I have been trying to figure out why my script is not working with no success. I greatly appreciate any help with this!
var fruit = [{title:"Apple"},{title:"Avocado"},{title:"Banana"},{title:"Cucumber"}];
var sections = [];
function createAlphabetSections(array) {
for(var i = 0; i < array.length; i++){
var firstLetter = array[i].title.charAt(0).toUpperCase();
var section = sections.filter(function (section) { return section.header === firstLetter;});
if(sections.length === 0){
sections.push([{header: firstLetter}]);
} else if (section.length > 0){
sections.push([{header: firstLetter}]);
}
}
}
createAlphabetSections(fruit);
I have an array with objects, and each object containing a property called name.
Now i want to have a second array containing the first letter of each name property from the first array. This first letter should be saved in a property called header if it doesn't exist yet.
I only want to have a letter to appear once, as this script is intended to be used for a list of alphabetic headers, each containing a list of names starting with the same letter.
I have created a function that uses the build-in JavaScript "filter" method, which is supposed to check if a value with this first letter already exists. But for some reason it doesn't work. The filter always returns an empty array, no matter what letter i provide.
I have been trying to figure out why my script is not working with no success. I greatly appreciate any help with this!
var fruit = [{title:"Apple"},{title:"Avocado"},{title:"Banana"},{title:"Cucumber"}];
var sections = [];
function createAlphabetSections(array) {
for(var i = 0; i < array.length; i++){
var firstLetter = array[i].title.charAt(0).toUpperCase();
var section = sections.filter(function (section) { return section.header === firstLetter;});
if(sections.length === 0){
sections.push([{header: firstLetter}]);
} else if (section.length > 0){
sections.push([{header: firstLetter}]);
}
}
}
createAlphabetSections(fruit);
Share
Improve this question
asked Nov 5, 2015 at 14:40
kleinermannkleinermann
552 silver badges10 bronze badges
2
- Please provide an example of expected function output. – halfzebra Commented Nov 5, 2015 at 14:42
-
The function should return an array of objects. Each object should have a property called header, which has the first letter of each title property from the first array assigned to it. Duplicates should be removed:
[Object { headerTitle="A"}, Object { headerTitle="B"}, Object { headerTitle="C"}]
– kleinermann Commented Nov 8, 2015 at 13:43
2 Answers
Reset to default 7First make use map to build a new array from the current array. Then filter out any duplicates.
var sections = fruit.map(function (item) {
// Return the first letter of the title property
return item.title.substring(0, 1);
}).filter(function (value, index, self) {
// http://stackoverflow./a/14438954/1789518
return self.indexOf(value) === index;
});
var getLetters = function(items) {
var letters = [];
items.forEach(function(item) {
if(letters.indexOf(item.title[0].toUpperCase()) === -1) {
letters.push(item.title[0].toUpperCase());
}
});
return letters;
};
getLetters([{title:"Apple"},{title:"Avocado"},{title:"Banana"},{title:"Cucumber"}]);