最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to create an array of all first letters from object properties in another multidimentional array? - Stack Overf

matteradmin4PV0评论

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
Add a ment  | 

2 Answers 2

Reset to default 7

First 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"}]);
Post a comment

comment list (0)

  1. No comments so far