An easy guide to creating a JavaScript function to locate books written by authors with less than 4 characters

Need assistance! Trying to figure out how to create a function that locates books with an author name containing less than 4 characters.

const books = [
        {
            "id": "a5b16ad1bb2e96b8c649da7150ad5726",
            "title": "Mon Oct 17 2022 21:32:42 GMT-0700 (Pacific Daylight Time)",
            "author": "Ana"
        },
        {
            "id": "221517d1a0948a2faa56a824803aa60e",
            "title": "bjlci",
            "author": "Lea Ann"
        },
        {
            "id": "286332e30d3962f5de20c2cf8b673482",
            "title": "Mon Oct 24 2022 00:38:43 GMT-0700 (Pacific Daylight Time)",
            "author": "Ana Lee"
        },
        {
            "id": "bd78bed71d601bdd19c76c8309287894",
            "title": "Mon Oct 17 2022 21:32:42 GMT-0700 (Pacific Daylight Time)",
            "author": "Lee"


    }
]

In progress: getting error message TypeError: Cannot read properties of undefined (reading 'length')

function shortNameAuthors(books){
    for (let i = 0; i < books.length; i++){
        if (books["author"].length <4){
            console.log(books[i]);
        }
    }
  }
  console.log (shortNameAuthors(books))

Answer №1

const getShortNamedAuthors = (library) => {
   const filteredLibrary = library.filter(
     record => record.author.length < 4
   )

   return filteredLibrary;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is an Ajax Call patiently anticipating the result of a separate Ajax Call?

I am facing a situation where I have two separate ajax calls that cannot be combined into one call. The first ajax call can start, and then the second ajax call can start either immediately or upon the user pressing a send button. However, the second ajax ...

Determine whether the response from a jQuery AJAX call is in JSON format or binary. If it is found to be binary, proceed

I am currently using ajax to retrieve and download an xlsx file from a PHP script. The script has two possible types of responses: JSON format if there are any warnings preventing the download (such as incorrect parameters), or the xlsx binary stream if ev ...

Tips for incorporating a set offset while utilizing the scrollTop() function

I have successfully implemented a code that sets a position:fixed to a div when it scrolls past the top of the screen. The code I used is as follows: var $window = $(window), $stickyEl = $('#the-sticky-div'), elTop = $stickyEl.o ...

Create an array in JSON format that includes a JavaScript variable, with the variable's value changing each time a mouse

var question="What is your favorite color?"; var option="red"; var col=[]; When the user clicks, the variable value changes and values should be pushed in a specific format. I am new to JavaScript, please help me with this. Thank you. //On click, the var ...

What is the best way to expand a div in a downward direction exclusively?

My task involves a parent div containing a centered child div. My goal is to have the child div grow downwards only upon clicking it, not in both directions. Initial state of the two divs: https://i.sstatic.net/cWYyV.png Outcome after clicking on div 2: ...

bcrypt is failing to return a match when the password includes numeric characters

I've integrated node-bcrypt with PostgreSQL (using Sequelizejs) to securely hash and store passwords. In the process, the user's password undergoes hashing within a beforeValidate hook as shown below: beforeValidate: function(user, model, cb) { ...

The Google Charts chartRangeFilter displays incorrectly upon reducing the height to a specific, seemingly random level

I am relatively new to web coding and currently working on a dashboard project for my client. I am using Google Charts to display the water level data that I collect. My issue is with the chartRangeFilter control - it works fine when the height is large en ...

Implementing ES6 Angular directives with two-way isolated binding

I'm really struggling to understand how isolating scopes function in my code. Interestingly, everything seems to work fine when I remove the scope part of the directive. Can someone please shed some light on what I might be overlooking? export func ...

Which is more effective: Layering multiple canvases in the DOM or consolidating all drawing on a single canvas?

In my previous projects, I have had experience working with layered canvases. While some of my colleagues swear by this approach, I find myself skeptical about its effectiveness. My primary concerns are: If multiple canvases are layered and one undergoes ...

How can I alter the icon's color?

Is it possible for the icon's color to change to red when the condition is greater than 0, and to gray when the condition is equal to zero? <TouchableOpacity onPress={() => { if (Object.values(selectedIt ...

Changing a byte array into a String in the realm of Java

Imagine we have an array called byte[]: byte[] data = {10,10,1,1,9,8} and the goal is to transform these values into a hexadecimal string: String arrayToHex = "AA1198" The question arises on how to achieve this using Java in IntelliJ. This might seem o ...

Ways to refresh a component on a webpage without having to refresh the entire page

I am looking to dynamically load a specific component on my webpage when another component is altered <input ..... onchange="callFunctionToReloadNextTag()"> <input ... /> <--This is the tag I want to reload Is it possible to reload th ...

Implementing Event Handlers for Multiple Textareas Using Jquery on a Webpage

The functionality of my script is exactly how I want it to be, but I am facing an issue when trying to replicate it on a page. The jQuery code manipulates textarea boxes based on button clicks, however, I now need each textarea box to have its own set of b ...

Search for documents using jQuery on the page

Here is the layout of a page: HTML <div class="alert alert-dismissable"> <div class="form-group text-center"> <div id="Section"> <div class="row"> <div class="col-md-12"> ...

Retrieve a property from a randomly selected element within an XML file using JavaScript

I am trying to retrieve the ID of an XML element, but I am struggling with accessing it. The element is selected randomly, so I am unsure how to obtain its information. To give you a better idea of what I am working on, here is a snippet of my code: I ha ...

Implementing a dynamic text field feature with JavaScript and PHP

Field Item                 Field Qty --------                 ----- --------                 ------ --------       ...

Clicking on text triggers image display

My journey in coding is just starting and I have a good understanding of the basics of HTML, CSS, Javascript, and jQuery. I am trying to make an image appear when I click on text but struggling with the implementation. I'm working on a restaurant web ...

Managing reverted MySQL transactions in a Node.js environment

I've been struggling with an issue for a few days now and I'm really hoping that you could provide some assistance. The problem lies in a node.js API using sequelize to interact with a MySQL database. When certain API calls are made, the code i ...

Segfault when working with a C++ dynamic array of structs

In the process of developing a program that extracts data from a file named "phonebook.txt" and exhibits it in the terminal, a query arises. This software prompts the user to specify the number of contacts to be added. Subsequently, upon user input, the sp ...

Navigate through two distinct elements by categorizing them based on their types

After completing the frontend design, I moved on to integrating the backend and encountered an issue. If anyone can offer assistance or even a hint, it would be greatly appreciated. Visit the demo website for more insight. Initially, I hard coded the comp ...