What is the data type of the $result.rows.item(x) variable in Javascript?

I have a function that reads and displays the contents of a SELECT query from WEBSQL in HTML5. I need to reuse this function, but I am facing an issue because it returns an Array of JSON objects and I want to convert it to rows.item(). Does anyone know how to achieve this?

For example, consider this JSON Array:

"retdic":[{"row_index":0,"lname":"Mato","age":26,"gender":"M","pic":"cyborg.jpg","fname":"Shibiru"},
          {"row_index":1,"lname":"Taro","age":30,"gender":"M","pic":"folder_wrench.png","fname":"Ichigo"},
          {"row_index":2,"lname":"Joni","age":27,"gender":"M","pic":"naruto.jpg","fname":"Perez"},
          {"row_index":3,"lname":"Sakura","age":24,"gender":"F","pic":"folder_table.png","fname":"Haruka"},              
          {"row_index":4,"lname":"Naruto","age":20,"gender":"M","pic":"naruto.jpg","fname":"Uzumaki"}]

Is there a way to convert it to something like $result.rows.item()? Since item() is not an array (as it should be item[] if it were), how can this conversion be done?

UPDATE

With Jeff's assistance and ideas, I was able to find a solution. Check out the live example

Answer №1

Let's start by comparing your array with the item() function. One is an array while the other is a function (assuming you use item() like "$result.rows.item(3)" to retrieve the 4th row). The solution is quite straightforward. Simply create a function that accepts a parameter 'i' and returns the item at index 'i' in the array. Here's how:

var myItemFunction = function(i) {
     return retdic[i]
}

Answer №2

To access the data in this object, you can use the following method:

$result.retdic[0].lname // 0 - index of the row

Alternatively, you can iterate through the object using a loop:

var i,item;
for (i in $result.retdic) {
  item = $result.retdic[i];
  console.log(item.fname)
}

By the way, if desired, you have the option to change the key 'retdic' to 'rows'

$res = {"rows":[
  {"name": "a"},
  {"name": "b"},
  {"name": "c"}
]};


var someKey = 'name',
valueOfSomeKey = $res.rows[rowNumberHere][someKey];

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

Using Angular JS services and controllers in separate files may result in errors

Within my angularjs project, I manage the following files: /index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-wid ...

JavaScript Conversion of Characters to ASCII Values

After converting a string input into a split array, I now need to convert that split array into ASCII for evaluation. Can someone provide guidance on how to do this? ...

Ways to remove items from Vuex store by utilizing a dynamic path as payload for a mutation

I am looking to implement a mutation in Vuex that dynamically updates the state by specifying a path to the object from which I want to remove an element, along with the key of the element. Triggering the action deleteOption(path, key) { this.$store.d ...

Matching numbers that begin with zero or are completely optional using Regex

Attempting to come up with a regex pattern that will allow the entry of the specified input into an HTML input field: The input must begin with 0 The input can be left empty and characters may be deleted by the user ^[^1-9]{0,1}[0-9\\s-\& ...

manipulating and merging a json data structure in PHP

Here is a json string that I need help parsing: { "6ad416d7-551a-41e3-9103-42d0a6a14d95":{ "0":{ "value":"KOSHER ONE" } }, "acb022a1-2369-4a20-8b6e-714387b81a6c":{ "0":{ "value":"PAREVE" } }, "f4ef2148-270e-4d9d-913d-f1a4ed2a73f0":{ "0 ...

increasing the size of an array in react javascript

componentWillMount(){ this.adjustOrder(); } componentDidMount(){} adjustOrder(){ var reorderedArray = []; this.state.reservation1.length = 9; for (var i = 0; i < this.state.reservation1.length; i++) { if(this.state.reservation1[i]){ r ...

Choose particular content enclosed by two strings (across multiple lines)

Looking to use regex to extract specific text between two strings. For example: foo I have four toys bar //single line foo //multiline I have four toys bar foo I have three pets bar foo I have three pets bar How can I extract the text between & ...

The radio button fails to trigger the setState function in React

In my React program, I have implemented a method to update the state and modify a specific value for a Radio button: The state : class App extends Component { constructor(props) { super(props); this.state = { checked: [], expanded: [], ...

Using regular expressions, eliminate the element from a JSON object that contains a specified property

Imagine I have a string representing a JSON object. It could be invalid, with some params that will be replaced by another system (e.g. %param%). The task at hand is to remove all objects with a known propertyName equal to "true" using regex. { "someT ...

Discovering the 3D coordinates of a point that is perpendicular to the midpoint of a line

(I am working with Javascript/Typescript and Three.js) Given two vectors, let's say {x:1, y:3, z:5} and {x:7, y:8, z:10}, I have a direct straight line connecting them. At the midpoint of this line, envision a disc with a radius of 1 that is perpend ...

Leveraging the spread operator in cases where the value is null

Is there a more elegant solution for handling null values in the spread operator without using if-else statements? In this specific case, I only want to spread assignedStudents if it is not undefined. When attempting to do this without using if-else, I e ...

ES6 throwing an error: SyntaxError due to an unexpected token ")"

Below is the Node.js script I am working on: pDownload("http://serv/file", "localfile") .then( ()=> console.log('downloaded file successfully without any issues...')) .catch( e => console.error('error occurred while downloading&ap ...

Retrieve the information from a website and display it on the current webpage using an ajax request

Is there a way to insert parsed HTML content into my webpage using just a link from another page? I'm attempting to use an AJAX call, but I keep receiving an error. Below is the code I've written, and the browser doesn't seem to be the issue ...

In the world of web development with JavaScript, jQuery, and EasyUI, we often encounter situations where the parameter

function formatData_original() { // convert obj_num2.formatter = function(value, rec) { var baseStr='&nbsp;&nbsp;' + rec.s_date + '<a class="easyui-linkbutton" href="javascript:void(0);" plain= ...

Express route encountered an error with an undefined value

Here's the method declaration: exports.postRedisValue = function(req,res) { let keyRedis = req.body.key; let valueRedis = req.body.value; console.log(keyRedis); //displays as undefined if(keyRedis && valueRedis){ ...

Limit the implementation of Angular Material's MomentDateAdapter to strictly within the confines of individual

Within my app, I have several components that utilize the mat-datepicker. However, there is one component where I specifically want to use the MomentDateAdapter. The issue arises when I provide it in this one component as it ends up affecting all the other ...

Send information using JSON from iOS to Rails server via HTTP POST

After successfully creating a post using an HTTP Client and setting the content type to application/json, I included the following json code: { "order": { "name": "Tia Carter", "location": "Corams", "phone_number": "707", ...

Laravel does not have the capability to provide a genuine json response

I am facing an issue with my Laravel controller. The code is straightforward: class MyController extends Controller { public function list() { return response()->json(['a' => 'hello']); } } When I access the ...

Ways to verify if any items in an array are present in a different array

I have two arrays of objects and need to determine if certain items are contained in another array. If any of them are not found, the result should be false. const arr = [ {full_name: 'Test'}, {full_name: 'Test1&apo ...

Deciphering a JSON field within a Spark dataset utilizing Spark

Input: identifier item_value 1 [{'dummyItem':'12346','itemRequest':{'code':'ITEM1', 'ATTR':'ABC'}}] 2 [{'dummyItem':'12347','itemRequest':{'c ...