Organizing data in a database the arrangement way

I'm looking to populate an array with values for "name" and "nickname" extracted from an SQLITE database and then display them in an alert box.

This task is part of a JavaScript project developed using Titanium Appcelerator.

Below is the code snippet:

var fightersRS = db.execute('SELECT * FROM fighters');

 var setFighters = [];

while (fightersRS.isValidRow())
{
  var name = fightersRS.fieldByName('name');
  var nickname = fightersRS.fieldByName('nickname');

      setFighters.push = {
             name: name,
             nickname: nickname           
             };

     fightersRS.next();
}
alert(setFighters);

The intended outcome is for the alert to display all values separated by commas, like so:

"muhammad ali, rocky balboa, ten shin han, etc ...."

My current code is not functioning as expected. Any advice on how to achieve the desired output?

Answer №1

To update your code, make the following changes:

var fightersList = db.execute('SELECT * FROM fighters');
var setFighters = [];
while (fightersList.isValidRow())
{
  var name = fightersList.fieldByName('name');
  var nickname = fightersList.fieldByName('nickname');

      setFighters.push({"name": name, "nickname": nickname}); //Ensure the correct syntax for pushing elements to the array

     fightersList.next();
}

To display the elements, utilize a for loop like so:

for(var i=0; i < setFighters.length; i++){
    alert("Name: " + setFighters[i].name + " Nickname: " + setFighters[i].nickname);
}

If you wish to show their names separated by commas, consider the following approach:

var fightersList = db.execute('SELECT * FROM fighters');
var setFighters = [];
while (fightersList.isValidRow())
{
    var name = fightersList.fieldByName('name');

    setFighters.push(name);

    fightersList.next();
}

var fighters = setFighters.join(","); //Concatenate array elements with commas
alert(fighters); //Will display all names separated by commas

Refer to the JavaScript join method for further information.

Answer №2

To concatenate all values with a comma, consider using a for loop or incorporating it inside a while loop.

Retrieving data from an array with comma separation may not be feasible. For reference, here's an example of an associative array on Stack Overflow. You can customize your loop or utilize the while loop you're constructing.

Learn more about associative arrays and fetching data here

Thank you

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

JavaScript allows for the insertion of values into either a textbox or dropdown menu

Is there a way to restrict the client from inserting both of these fields: either selecting from a textbox (selected Column) or choosing a value from a dropdown menu (selected Column), but only one field is allowed in a gridview? <asp:GridView ID="gvMe ...

What is the best method for transforming a base64 encoded string into a base64 formatted PDF string?

Could someone please help me with a problem I'm facing? I am utilizing an AngularJS .pdf viewer that displays documents in a modal using base64. Everything works smoothly when the base64 is generated from a .pdf file. The backend (Java) generates th ...

javascript - Add a function to an array, iterate through, and delete once executed

I've been attempting to develop an array that stores all pending error messages appearing on the DOM using jQuery. My goal is to iterate through the array to check for any error messages to display, and then remove them after execution. However, I&ap ...

Looking to spice up your static HTML site? Dive into the world of Ruby on

I recently developed an app that features a static HTML webpage containing text and images, and now I'm interested in incorporating Ruby on Rails to explore its capabilities further. After creating a basic RoR application, I copied the HTML content f ...

What could be causing the 304 error when using $http.get?

I'm a newcomer to angular and facing an issue with a service that was functioning perfectly, but suddenly stopped. The service I am referring to has the following method. this.retrieveForms = function() { return $http.fetch("/forms"). then(fu ...

Leave a message | Google Sheets | JavaScript using nodeJS

I am having trouble adding comments to cells using the "Google Spread-Sheet" module in NODEJS. I have successfully written to cells, read from them, and deleted them, but I can't figure out how to add a comment to a cell. The tutorials I have found on ...

Ordering an Array of JavaScript Objects in a Custom Sequence (utilizing pre-existing methods)

Imagine we have an array of objects: ["c", "a", "b", "d"] Is there a way in ECMAScript or through a third-party JavaScript library to rearrange the objects in the first array to match the order specified by the second array, all within one line or functi ...

Activating a tab using a JS call in Bootstrap with the data-toggle attribute

My JSP page is using bootstrap's data-toggle="tab" functionality to display tabs. When the page loads, one tab is made active by default. <ul class="nav st-nav-tabs"> <li class="active"><a href="#tab1" data-toggle="tab">First Ta ...

Encountering a 500 internal server error after deploying due to utilizing getServerSideProps in Next.js

When trying to redeploy my website on Vercel, I added getServerSideProps to my index.js file. However, I encountered an error that I am not familiar with. The program works perfectly on localhost. This is the getServerSideProps code that I added: export a ...

Utilize AngularJS to retrieve and interact with the JSON data stored in a local file once it has

Please do not mark this as a duplicate since I have not found a solution yet. Any help would be appreciated. I have a file called category.json located next to my index.html file, containing the following JSON data: [{"name":"veg"},{"name","non-veg"}] W ...

Facing an issue in React-Native where unable to navigate to the second screen

I hope you don't mind the length of my query, as this happens to be my debut here and I am really eager to unravel the mysteries surrounding my code conundrum. Your assistance in this matter would be greatly appreciated. Could someone please lend me ...

Adding a plane to a Three.js scene

When attempting to introduce a plane into the scene, I noticed that nothing is added when checking the children's scene. var newPlane = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffff00, opaci ...

What is the best way to determine the number of items in a class's property using the Swift programming language?

I need assistance with creating a method to calculate the total price of items stored in a property of a class. Programming is not my strong suit, so any help would be greatly valued. Thank you in advance for your help. ...

Utilize AJAX within an Angular method

I am encountering 2 issues with the $http function in this particular code snippet. $scope.buttonClick = function(){ // Sending input data $.post('lib/add_contact.php', $scope.contact, function(data){ data = angular.fromJ ...

Mastering the art of curating the perfect image for your Facebook timeline

Controlling the Like image presented on Facebook timeline can be done using the header element. In my single-page app, I have multiple like buttons, each should be connected to a different image. What is the best way to associate a specific image with e ...

What is the process of calculating the difference between two time values in PHP?

I have searched everywhere online and tried multiple methods over the past couple of days, but still can't seem to achieve the desired result. My goal is to subtract two different times, for example 22:00:00 - 00:30:00 = 21:30:00 $hourToEatLastMeal = ...

Animating a dropdown menu with jQuery

I have a typical submenu structure set up like this: <ul> <li> <a href="#">Parent link</a> <ul> <li><a href="#">Submenu link</a></li> </ul> </li> </ul> In my de ...

Retrieving data from JSON in JavaScript

Extracting data from a JSON file and using a JavaScript script to visualize it through a graph. JSON file link: https://i.sstatic.net/1gqov.png The JavaScript code responsible for drawing the graph is as follows: $(document).ready(function(){ google. ...

Tips for assisting flow with managing the initialization of react component state

In my React components, I am initializing state like this: export default class LoginForm extends Component { state = { // (*) flash: { message: null, style: null } // initialiser for flash message: show no ...

Unable to display the retrieved list data using v-for loop from axios get request

I recently started using Vue.js and decided to create a simple HTML website with Vue.js to display data from myphpadmin using axios. Everything was working smoothly until I encountered an issue where I couldn't display the JSON message on my screen. n ...