Applying VueJS filters to refine object values

Is there a way to verify if the value of "startAt" is present in an object and return its corresponding ID?

Here is the object in question:

[{
  "id": "1234567",
  "createTimestamp": "2020",
  "name": {
    "action": "",
    "allDay": false,
    "category": "Misc",
    "startAt": "05",
    "title": "foo"
  },
  "updateTimestamp": "2020"
}]

Below is my progress so far. The filtering works, but I'm struggling to access and return the ID.

<div v-for="(hour, i) in 24" :key="i">
  {{ filterByHour(hour) }}
</div>
filterByHour(id) {
  if (id < 10) {
    id = 0 + id
  }

  const result = this.events.filter(item => item.name.startAt === id.toString())
  return result
}

Any suggestions on how to successfully return the ID of the object?

Answer №1

  1. The issue arises in the line id = 0 + id where id is evaluated as 5 instead of 05 when id equals 5. To resolve this, update it to id = '0' + id to ensure conversion to a string.

  2. Following the filter operation, utilize the map function to extract the id.

const events = [
  {
    id: "1234567",
    createTimestamp: "2020",
    name: {
      action: "",
      allDay: false,
      category: "Misc",
      startAt: "05",
      title: "foo"
    },
    updateTimestamp: "2020"
  }
];

function filterByHour(id) {
  if (id < 10) {
    id = "0" + id;
  }

  const result = events
    .filter(item => item.name.startAt === String(id))
    .map(x => x.id);
  return result;
}

console.log(filterByHour(5));
console.log(filterByHour(11));
console.log(filterByHour());

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

trouble encountered when attempting to integrate typeahead functionality in AngularJS using jQuery

Greetings! I am brand new to using AngularJS and currently exploring the implementation of typeahead functionality. I decided to utilize an existing library by including the following script: <script src="lib/xyz/typeahead.bundle.js"></script> ...

Ensured that the class located outside of the main element is being correctly managed

What is the best way to handle a checked attribute in Vue.js? <input :id="'todo'+id" type="checkbox" :checked="isPacked ? 'checked' : ''"> This code snippet will help clarify that The text "'checked' : ...

Show only particular elements from an array that satisfy specific conditions

I'm dealing with a challenge related to extracting specific items from an array and displaying them only if they meet certain criteria. To provide some background, I have an array called Person which is filled with data from an excel file. Each eleme ...

Extract the value from JSON data

I am faced with the challenge of extracting the value of slug from each child in a JSON dataset. The issue lies in the fact that I cannot predict how many children will be generated whenever new data is received. The generation of nested children is dynam ...

Live notification application using node.js

I am looking to create a recipe maker webapp for practice purposes. This webapp will consist of 2 main pages: the index page and the recipes page. On the "index" page, users can create their own recipes. On the "recipes" page, users can view a table ...

Trouble with 'this.function' and handling scope within my code

Hi there! I'm having an issue with this code snippet: Whenever I reach line 109, I encounter an error message that reads "TypeError: Result of expression 'this.allVarsDefined' [undefined] is not a function." I find scope in javascript to b ...

The best approach to incorporating interactive animation in next.js

My vision is to develop a character creation application using next js. The app should empower users to customize the character using sliders and gender selection buttons. The ultimate goal is to have a 2D animated version of the character that dynamicall ...

Utilizing class references within a method

I have been developing a script that is designed to dynamically load content into multiple predefined DIVs located in the topbar section of my website. Within the Topbar Object, there is an object called Ribbon which contains functions for manipulating on ...

The art of creating an asynchronous function: A comprehensive guide

My goal is to download files from a Firebase bucket and then store them in a database. I need the download process to be asynchronous, ensuring that each file is fully downloaded and added to an array before moving on to the next one. However, my current ...

When the button is clicked, my goal is to increase the value of the text field by one using JavaScript

I need the functionality for each button to increment the value text field located next to it <input type="number" value="0" min="0" max="5" step="1" id="qty"/> <button onclick="buttonClick()" type="button" class="btn btn-success btn-sm" id="add" ...

Exploring Highcharts Pie Chart with AJAX for Real-time Data Updates

Looking for some guidance with implementing AJAX in my code to make my chart dynamic based on data from the database. Although the code is error-free, the chart is not refreshing automatically. Any help, comments, or suggestions would be greatly appreciate ...

The echo statement is failing to show any value following the ajax request

I am in need of assistance. I would like to output the value from a PHP echo statement using an AJAX call. Currently, the code is functioning without any errors. When I select options from a dropdown menu, the value is displayed on the console by using con ...

Error: Unable to register both views with identical name RNDateTimePicker due to Invariant Violation

Encountering an issue while attempting to import: import DropDownPicker from 'react-native-dropdown-picker'; import DateTimePicker from '@react-native-community/datetimepicker'; <DropDownPicker zIndex={5000} ...

Basic $http.get request including parameters

I've been attempting to send an HTTP request using the AngularJS $http service like this: $http.get('http://myserver:8080/login?', { params: {username: "John", password: "Doe" }, headers: {'Authorization': ...

Ways to initiate a transition upon clicking a button, causing it to switch from being hidden (`display: none`) to visible (`display: block`)

I'm attempting to create a smooth transition effect when a button is clicked, similar to a toggle function, where the content below it seamlessly shifts instead of abruptly moving. The classic example of this is the Bootstrap navbar hamburger menu, wh ...

You won't be able to view over 15 KML layers on a page that relies on Google Maps API

I've encountered an unusual issue: I need to create multiple KML layers from separate KML files and display them on a single map window. The number of files ranges from just a couple to less than fifty. Currently, my code works perfectly when given 1, ...

Stuffing a container with an image without considering the proportions

I am experimenting with filling a parent <div> with an image without concern for proportions. Despite knowing that it may not look great at all sizes, I just want to test its feasibility. Currently, the image is scaling instead of stretching to fit m ...

Error: The specified module is missing an export that was requested

Hello, I encountered an error in my console that reads: "Uncaught SyntaxError: The requested module './components/tileHeader/controller.js' does not provide an export named 'tileHeader'". Below is the code snippet causing the issue: co ...

Guide to setting up a default route that precedes all other routes in Express.js routing

I'm struggling to articulate this question correctly, so please be patient with me. Currently, I have a few routes set up: localhost:3000/index localhost:3000/home localhost:3000/login localhost:3000/forgot However, I would like to add a client n ...

Retrieving a page using jQuery

Below is the JavaScript code that I am using: $.ajax({ url: "test.html", error: function(){ //handle error }, success: function(){ //perform actions here with the received data } }); After retrieving test.html, I am wo ...