admin管理员组文章数量:1435261
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"}]);
本文标签:
版权声明:本文标题:javascript - How to create an array of all first letters from object properties in another multidimentional array? - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745650155a2668378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论