The JavaScript filter function will only return arrays that have matching IDs

How can I filter out book data based on the author's id? I have a list of books with various author ids, and I want to only return the books that match a specific author id. However, my current filtering method doesn't seem to work correctly as it always returns all book data.

const author = "AUTHOR#e9bb9d29-7f20-4fce-892c-6a155dbee42c";

const Book = [
  {
    publishingYear: "2020",
    rating: 5.2,
    GSI1SK: "AUTHOR#a731ea70-f3f3-4811-9734-f22c0856385d",
    genre: ["adventure", "drama", "scifi"],
    GSI1PK: "AUTHOR",
    page: 100,
    publisher: "Afternoon pub",
    SK: "BOOK#c4a58f20-4977-4db8-9723-0185f68cdf01",
    price: "3.50",
    PK: "BOOKS",
    author: "Krishna",
    title: "Me and mySelf"
  },
  {
    publishingYear: "2020",
    rating: 5.2,
    GSI1SK: "AUTHOR#6b7c10ff-0e2c-46bd-9697-3b51730d8b29",
    genre: ["adventure", "drama", "scifi"],
    GSI1PK: "AUTHOR",
    page: 100,
    publisher: "Day pub",
    SK: "BOOK#e4773a32-5451-42c6-a3f1-a6aa45176256",
    price: "3.50",
    PK: "BOOKS",
    author: "John doe",
    title: "Hello world"
  },
  {
    publishingYear: "2020",
    rating: 5.2,
    GSI1SK: "AUTHOR#a731ea70-f3f3-4811-9734-f22c0856385d",
    genre: ["adventure", "drama", "scifi"],
    GSI1PK: "AUTHOR",
    page: 100,
    publisher: "Night Pub",
    SK: "BOOK#fb56a876-41bc-49f9-9762-c48e90af3117",
    price: "3.50",
    PK: "BOOKS",
    author: "Krishna",
    title: "Amazing Race"
  }
];

const Books = Book.filter((i) => {
  console.log(i.GSI1SK);
  i.GSI1SK === author;
  return i;
});

console.log(Books);

Answer №1

It seems like you might need to adjust your approach with the filter function. Make sure to carefully evaluate the condition being checked and provide true or false accordingly.

const Books = Book.filter((book) => {
  console.log(book.GSI1SK);
  return book.GSI1SK === author;
});

Answer №2

Excluding superfluous lines is a good practice. Give this a shot.

const Library = Books.filter(item => item.GSI1SK === author)

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

How to utilize FileReader for parsing a JSON document?

I'm currently facing an issue while attempting to read and copy a JSON file uploaded by the user into an array. When using .readAsText(), the returned data includes string formatting elements like \" and \n. Is there a way to utilize FileRe ...

The connection between MongoClient and Express has been established successfully, however, an error occcurred: TypeError: Cannot read the property 'collection' of null

I am currently working with the MongoClient instead of mongoose, but I am facing an issue where I can't seem to set a new collection in my routes file. db/index.js const {MongoClient} = require('mongodb'); const MONGO_DB_NAME = 'mooo ...

Adjust the size of items using a background image that covers the element

I'm struggling with a seemingly simple task here. I've been experimenting with different methods, but I just can't seem to grasp it. On the front page of my website, there is a cover section that contains a logo and a button: <section i ...

Responsive React Page Design with Swiper's Breakpoints (Bootstrap Integrated)

I have integrated Swiper React with my React project. It seems that I cannot manipulate the 'slides per view' using Bootstrap directly. If it were possible, I would define the necessary columns and insert the swiper slides within them. Currently ...

The functionality of string replacement is ineffective in the Safari browser

When using the code "MM/DD/YYYY".replace(/.?YYYY.?/, ''); , Chrome displays MM/DD, while Safari shows an empty string. Why does this difference occur? Is there a method that will produce consistent results across all browsers? ...

Client-side resizing an image before sending it to PHP for uploading

Greetings! Currently, I am utilizing a JavaScript library from this source to resize images on the client-side. The image resizing process works successfully with the following code: document.getElementById('foto_select').onchange = function( ...

When running 'npm run dev', an error occurred after the SizeLimitsPlugin emitted at 98%. The specific dependency that was

I encountered an issue while trying to incorporate Google Maps into my Laravel project and received the following error message after running npm run dev: 98% after emitting SizeLimitsPlugin ERROR Failed to compile with 1 errors 10:52:34 PM This dependen ...

Tips on customizing the appearance of mat-card-title within a mat-card

Is there a way to truncate the title of a mat card when it overflows? I tried using the following CSS: overflow:hidden text-overflow:ellipsis white-space: nowrap However, the style is being overridden by the default mat-card style. I attempted to use mat ...

What is the best way to enable horizontal scrolling for textarea overflow in a smooth manner like input, without any sudden jumps?

Currently, I am interested in utilizing a one-line textarea (with rows=1 and overflow-x:hidden;) similar to input type="text. However, I have encountered an issue where the content scrolls with "jumps" while typing inside it: https://i.stack.imgur.com/Rzf ...

What is the reason for IE displaying null when the model does not exist?

Why does IE 11 render 'null' if my model does not exist? For instance: <tr> <td [innerHTML]="model?.prop1 | my-pipe"></td> </tr> Imagine this scenario: When the page loads, a request is sent to the server and the res ...

Creating a User Authentication System using Node.js and Express

I am currently working on developing an application with node.js and expressJs, even though I come from a PHP background. In PHP, we typically use the session to handle logins, so naturally, I thought that in the case of node.js it would be similar. After ...

Issues with CSS properties not functioning correctly within the class.col function

When attempting to apply specific CSS properties to a particular area (e.g., "Dashboard") by assigning the class name "main" to the div.col function in the HTML, I encountered an issue where the CSS property applied affected the entire page. The following ...

How to implement bubble sort for a three-dimensional array in C++?

I am interested in using a bubble sort to sort data from an external data file that looks like this: 00000002 45 56 32 46 00000001 87 95 83 100 The goal is to have all the data under "00000001" appear before the data under "00000002". To achieve ...

Incorporate additional text to the downloads hyperlink using jquery

Is it possible to achieve this using jQuery since I am unable to directly modify the HTML markup? I am looking to append download.php?file= to a URL without replacing the entire href attribute. Here's an example of what I'm trying to achieve: & ...

Searching for attributes in a JSON document

Currently, I am dealing with a results API that provides me with a JSON file. My goal is to search for a specific property within this file. To achieve this, I first push the JSON data into an empty array and then iterate over it to retrieve the first obje ...

Difficulty in adjusting the height of the popover menu that appears when using the Select Validator in Material UI

I am having trouble adjusting the height of a popover that emerges from a select validator form in Material UI. Despite attempting various methods, including adding the following CSS to the main class: '& .MuiPopover-paper': { height: &apos ...

Getting the Correct Information from Upload Files in jQuery and PHP

While experimenting with the jquery plugin called jquery.filer, I encountered an issue during file upload on their official website . The returned value I received looked like this: { "files": [ "..\/uploads\/dK079QrL2g.docx" ], "metas": [ ...

What could be causing issues with my JavaScript AJAX?

I'm in the process of developing a basic chat system that automatically loads new messages as they come in. Initially, I used to fetch all messages from the database. However, I encountered an issue where the scroll bar would constantly jump to the bo ...

Utilizing i18next-express-middleware for seamless language switching based on user input

As a newcomer to node.js, I am looking to internationalize my application. To kickstart this process, I have utilized the template available on i18next-express-middleware github in conjunction with resources from the i18next website. My goal is to enable u ...

The console is showing the Ajax Get request being logged, but for some reason it is not displaying on the

Could someone please explain why this response isn't displaying on the page? $.ajaxPrefilter( function (options) { if (options.crossDomain && jQuery.support.cors) { var http = (window.location.protocol === 'http:' ? &apos ...