Decoding a Json list with angularJS

I have a JSON dataset structured as follows:

 $scope.jsondata = [{filename:"/home/username/textfiles/0101907.txt"},{filename:"/home/username/textfiles/0124757.txt"},{filename:"/home/username/textfiles/0747332.txt"} ... ];

Here is my HTML code:

<li ng-repeat="x in jsondata">
   {{x.filename}}                     
</li>

When running the above code, I am retrieving the full file path such as "/home/username/textfiles/0101907.txt". However, what I really need is just the file name, like "0101907.txt", without the entire path. Can anyone assist me with parsing the JSON array to split the path and extract only the file name? Thank you.

Answer №1

To extract the file name from each path in the array, you should split the string using "/" and then use the pop() method.

<li ng-repeat="x in jsondata">
   {{x.filename.split("/").pop()}}                     
</li>

Answer №2

To make updates, utilize the power of regex. Set up a controller method such as retrieveFileName and integrate it within your HTML code.

function retrieveFileName(path) {
   return path.replace(/^.*[\\\/]/, '');
}

HTML Integration

Implement it as shown below:

<li ng-repeat="item in jsonData">
   {{retrieveFileName(item.filename)}}                     
</li>

Answer №3

To achieve this, I recommend utilizing the .lastIndexOf method in JavaScript. Here's an example of how it can be done:

<li ng-repeat="x in jsondata">
   {{x.filename.substr(x.filename.lastIndexOf('/')+1}}                     
</li>

Answer №4

Here is the solution:

<li ng-repeat="item in data">
{{item.name.split("/").reverse()[0]}}
</li>

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 it possible to adjust the width of a parent element based on the number of child

In this particular example, I have structured a header with a logo-container positioned on the left side, a menu in the center, and a button on the right. The menu consists of 5 top-level items and 2 sub-menus. <div class="container"> <div cla ...

The enigma of AngularJS

It seemed like I was starting to understand AngularJs, but now I realize I still have a lot to learn... I attempted to create a simple demo showcasing the ng-repeat directive, and suddenly nothing is working - all I see is a blank screen with no explanati ...

When displaying JSON data in an Android app, the ListView is only displaying a single row

I am working on displaying JSON data in a ListView within an Android app. I am able to retrieve the JSON data successfully, but when attempting to display it in the ListView, only one row is showing up. Additionally, I want each row’s data to be shown in ...

Efficient JSON management for requests and responses in Grails

I pondered whether there is a universal method to parse requests in a standardized manner or append fields to a JSON response for every transmission. It would be ideal for me to handle and interpret something like this: { transactionId:456, // includ ...

There seems to be a glitch in my PHP comment validation function—it does not seem to be functioning

This form is contained in a PHP file named single.php. <form action="comments.php" method="post" > <?php include(ROOT_PATH . "/app/helpers/formErrors.php"); ?> <input type= "hidden" name="id" value= & ...

Prevent scrolling on both md-sidenav and md-content in AngularJS Material

Currently, I am working on a website using AngularJs Material sidenav. My goal is to make both md-sidenav and md-content appear as one page, without any scroll bars showing up when the height of one element exceeds that of the other. How can I achieve th ...

When multiple checkboxes are selected, corresponding form fields should dynamically appear based on the checkboxes selected. I attempted to achieve this functionality using the select option method

Require checkboxes instead of a selection option and have multiple checkbox options. Depending on the checked checkboxes, different form fields should appear. A submit button is needed. I have included some CSS code, but a more detailed CSS code is requir ...

able to move through the content without displaying the scrollbar

I am looking to keep my content able to scroll, but I don't want the scrollbar to be visible. I have written my CSS like this: #content-three{ width:100%; height:100%; position:relative; overflow:hidden; } .block_s{ width:90%; ...

How can you enable fullscreen for a featherlight iFrame?

I have implemented Featherlight to display an iframe within a popup modal on my website. If you click the iframe button, you can see a demo of how it works. One issue I am facing is that the generated iframe tag by Featherlight does not include allowfulls ...

Extract website information, transform into JSON format, input into SQL database?

I have this interesting project where I am seeking to extract data from an external webpage, transform it into a specific structure, and then store it in a database. The purpose of doing this is purely for enjoyment - I'm essentially replicating an e ...

"Using jQuery's animate() function to dynamically change text content

I'm currently venturing into the world of jQuery animate() and decided to create a presentation-style project for practice. However, I've hit a roadblock when attempting to alter the text within my div element in the midst of an animation utilizi ...

Trouble displaying data with Angular 6 JSON object pipe

Having an issue with displaying tasks from a MongoDB project schema in HTML. The tasks are stored in a nested array and I want to show the task name and description. Here's the code: <div class="card-body"> {{project.taskName | json}} </ ...

Sending requests through RoR and receiving JSON responses - what's next?

Hey there! So I'm diving into the world of Ruby on Rails and I've managed to create a form that sends data to an external widget which then returns JSON. Here's what my form looks like: <%= form_for :email, :url => 'http://XXX.X ...

Storing data in a text or HTML file using server-side JavaScript

Currently, I am working on a JavaScript form that involves saving user-entered variables to either a .txt file or a new webpage with the variables pre-filled in the inputs. I know that JavaScript cannot directly manipulate the user's machine, but I am ...

The bootstrap navbar dropdown feature isn't functioning properly on iPhones

Currently utilizing "bootstrap": "~3.3.4" within the mean.js framework, I am facing an issue with the navbar dropdown menu. On desktop, everything functions as expected - the dropdown opens and remains open when the icon is clicked. However, once deployed ...

"Obtain a DOM element within an Angular directive by using the jQuery find method

When inspecting the DOM, I can see the element anchor tag present but cannot retrieve it using jquery.find(). The console returns a length of 0, preventing me from initializing angular xeditable on that element. angular.module('built.objects') ...

Tips for keeping div and input tags selected even after a user clicks

I am working on a quiz script and I am trying to customize it so that when a user clicks on the div or input tags, they remain selected with a different background color. Currently, I have achieved changing the background color of the div when clicked, ...

Using Javascript, the sum of inputs is calculated based on the keyup event

I am a beginner in javascript and I'm learning about events. Below is the HTML code for a table that displays income titles and their respective prices. <table class="table table-hover table-bordered" id="incomeId"> <thead> <tr&g ...

Managing several instances of NgbPagination on a single webpage

I am facing a challenge with having multiple NgbPagination components on a single page. For more information, please visit: Initially, I attempted to use ids but encountered an issue where changing one value in the first pagination affected both tables. ...