An easy guide to sorting outcomes using JSON

I have JSONResults in my Controller that contains all the data from a table. On the client's HTML detail view page, I am using JavaScript to fetch this data. How do I extract data from JSON where the client name is equal to klID (this is a JSON string with the client's name)?

I have attempted the following code but it retrieves all the data:

for (var i = 0; i < json.length; i++) {
    $.data = (json[i].klID = "@Html.DisplayFor(model => model.klient)");
    var dateString = json[i].datum.substr(6);
    var currentTime = new Date(parseInt(dateString));
    var month = currentTime.getMonth() + 1;
    var day = currentTime.getDate();
    var year = currentTime.getFullYear();
    var date = day + "/" + month + "/" + year;

    tr = $('<tr/>');
    tr.append("<td>" + json[i].pID + "</td>");
    tr.append("<td id='date'>" + date + "</td>");
    tr.append("<td>" + json[i].description + "</td>");
    tr.append("<td>" + json[i].popust + "</td>");
    $('table').append(tr);

Please help me. Thank you.

Answer №1

The code provided does not seem to be relevant to your issue as there is no filter mentioned here. If you only want to display items where kIID equals 'client', you can use the following if condition:

if(json[i].klID === 'What ever name'){
    var dateString = json[i].datum.substr(6);
    var currentTime = new Date(parseInt(dateString));
    var month = currentTime.getMonth() + 1;
    var day = currentTime.getDate();
    var year = currentTime.getFullYear();
    var date = day + "/" + month + "/" + year;

    tr = $('<tr/>');
    tr.append("<td>" + json[i].pID + "</td>");
    tr.append("<td id='date'>" + date + "</td>");
    tr.append("<td>" + json[i].description + "</td>");
    tr.append("<td>" + json[i].popust + "</td>");
    $('table').append(tr);
}

Answer №2

ES6 introduces a powerful filtering method:

let data = ['apple', 5, 'banana', 8, 9];
let result = data.filter((item) => (
  isNaN(parseInt(item))
);
console.log(result); // displays: ['apple', 'banana']

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

Looking to utilize Python Selenium for downloading a PDF file

I am currently working on automating the process of downloading PDFs using Selenium Webdriver in Python An issue I've encountered is that the download button is hidden within an embed tag in the HTML code <embed width="100%" height="100%" name="p ...

HTML5 Dragend event failed to trigger in Firefox

While working on my project, I encountered an issue where the 'dragend' event is not fired in Firefox 22.0 on Ubuntu but works perfectly fine in Chrome. I have written a logic to make changes on drag start and revert them if drop fails, triggered ...

Loading STL files from a buffer instead of a path within Three.js

I'm struggling to showcase a user-uploaded STL file in Three.js. The issue arises when my file is delivered to the front-end through a GET request: res.sendFile(path). Unfortunately, I can't directly utilize this raw data to load the file withou ...

Detect and switch the value of a CSS selector using Javascript

Incorporating the subsequent jQuery code to insert CSS styles into a <style id="customizer-preview"> tag within the <head>: wp.customize( 'site_title_color', function( value ) { value.bind( function( newval ) { if ( $( &a ...

What is the best way to sequence the functions in an AJAX workflow?

I'm currently working on optimizing the execution order of my functions. There are 3 key functions in my workflow: function 1 - populates and selects options in a dropdown using JSON function 2 - does the same for a second dropdown function 3 - ...

Is there a way to trigger a function upon the loading of a template in Angular 2?

I'm a newcomer to angular2 and I need to trigger a function when a template loads or initializes. I have experience with achieving this in angular1.x, but I'm struggling to figure out how to do it in angular-2. Here's how I approached it in ...

Invoking a Directive within another Directive

Feel free to check out this demo on Plunkr. I've set up a basic structure: <body ng-app="myApp"> <div ng-controller="myController"> <parent-directive></parent-directive> <child-directive></child-direc ...

Utilizing Angular to render JSON data on the screen

Currently, I have my data saved in a database and am retrieving it in Angular. The problem arises when I attempt to display the data as I encounter an error. All I want is to exhibit the question in HTML using ngFor. Being new to Angular, any advice or gui ...

Capturing all requests - javascript

I have a webpage called sample-page.html that contains 2 links: sample-page.html - link1 (GET, AJAX) - link2 (GET, new page) Clicking on link1 triggers an AJAX GET request and remains on the same page (sample-page.html). Clicking on l ...

Having trouble with rendering components in React and Bootstrap?

Attempting to display basic Bootstrap components using React. This corresponds to the index.html file: <!doctype html> <html> <head> <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script> <script src="j ...

Managing uncaught exceptions in node.js

While working on my project, I encountered an issue with catching exceptions when opening a connection using mongoose.createConnection. Here's the code snippet causing the problem: var createdDb = mongoose.createConnection(connectionString); createdD ...

Intermittent issues with requesting JSON data

My current project involves building a script that iterates through a list, retrieves product SKUs, and includes them in a JSONP request to retrieve an object. The script seems to be functioning as intended, but there are occasional failures. Here is an e ...

Redux does not have the capability to insert an object into an array

I'm currently learning about redux and I've encountered an issue trying to add multiple objects into the initialState array. I attempted using the push() method, but it isn't working as expected. The submitter value is being passed to my act ...

Adding several <div> elements with the correct indices

I need help with a dynamic form that requires users to select a state before revealing the corresponding cities within that state. <form method="post"> <div class="summary"> <div class="trip"> <select name="State" class="s ...

Guide on retrieving Images from an express API using mongo DB

My API includes the quoteImage key which contains the path to the image stored in the backend. If you want to see the image, click here: API image I am currently working on my react application and struggling to display the images despite being able to sh ...

Having difficulty breaking down values from an object

Attempting to destructure the data object using Next.js on the client side Upon logging the data object, I receive the following: requestId: '1660672989767.IZxP9g', confidence: {…}, meta: {…}, visitorFound: true, visitorId: 'X9uY7PQTANO ...

maximum number of results in google custom search limit

I'm trying to retrieve the top 40 results from the Google API, but when I limit the result using the code below, it doesn't seem to work. How can I achieve getting the top 40 results with the Google API? <script> (function() { ...

JQuery Ajax: Issue with updating Total Likes for posts when Like button is clicked

There are two JQuery Ajax calls in this scenario. The first call retrieves user messages from MySQL, along with the number of likes for each message and a button for users to like the message. This initial request is functioning correctly. The second Ajax ...

Transforming button properties into a JSON format

I am currently in the process of developing a web application using node.js and mongodb. Within my app, there is a table that dynamically populates data from the database using a loop. I encountered an issue with a delete function that I implemented base ...

Creating rows within a table in React.js using the map method: Techniques to follow

Here is my code snippet: const [tasks, setTasks] = useState(''); I am simulating data with a mock server. function fetchTasks() { axios.get('http://localhost:4000/tasks') .then(function (response) { ...