extract data from an array

Hey there, I could really use some assistance with the code snippet below. My goal is to map and store the values in an SQL database. Unfortunately, I'm struggling to extract the values from the array ttNieuweSessie.

As a result, all the values are showing up as undefined. Does anyone have any suggestions on how to resolve this issue?

msg.topic = ttNieuweSessie.map(function(){
    return 'INSERT INTO tblSessies (idSessie, BeginTijdms, idApparaat) VALUES ('+idSessie+', '+BeginTijdms+', '+idApparaat+') ';
});

Answer №1

To access the currentValue, you need to include a parameter in the function that you have designated as the callback for the Array.map() method.

var queries = ttNieuweSessie.map(function(val){
        return 'INSERT INTO tblSessies (idSessie, BeginTijdms, idApparaat) VALUES ('+val.idSessie+', '+val.BeginTijdms+', '+val.idApparaat+') ';
});

Afterwards, your queries variable will hold an array with a query corresponding to each element in ttNieuweSessie

Answer №2

    msg.topic = ttNieuweSessie.map(function(item){
       return 'INSERT INTO tblSessions (sessionID, startTimeMs, deviceID) VALUES ('+item.sessionID+', '+item.startTimeMs+', '+item.deviceID+') '; 
    })

You have the option to pass the current item to a function within the map method

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

The MySQL database ended up containing encoded characters

I am facing an encoding issue - the data in my MySQL table is not displaying correctly. Some of the columns have ended up with unfamiliar characters including oblesks, negation signs, and question marks inside diamonds due to incorrect encoding. I don&apos ...

The latest images are now visible in the table alongside the existing images that were previously added

When inserting images here, previously added images are displaying. Any solutions? Here is my view page <div class="col-md-2"> <form enctype="multipart/form-data" method="post" action="<?php echo base_url();?>admin_control/upl ...

Encountering an issue where properties of undefined cannot be read (specifically 'img') following an update to the img value using redux toolkit

Hello! I have a project built with MERN and redux-toolkit (using an older version as per the tutorial). On the product page, I am trying to update a selected product. The update route runs without any errors, but after updating, I encounter an error that s ...

Correcting Typing Errors in TypeScript

My current challenge involves a method that is consuming data from an external API: public async getStatus(id: string): Promise<APIRes | undefined> { try { const result = await getRequest(`${this.URL}/endpoint/${id}`) const respo ...

Efficiently transferring data from a cron job to a Laravel PHP controller

Is there a way to transfer data from a cron job to a Controller that generates JSON in Laravel? Here is my route: Route::get("/data",'DataController@Data'); And this is the Data function in my controller: public function Data(){ return res ...

What are the steps to fetch JSON data from a different domain server using AJAX?

I'm facing an issue with the API I'm using for my ajax call. It returns json and does not support jsonp, which unfortunately cannot be changed. Every time I try to use the code snippet below, I encounter a 'missing ; before statement' e ...

What is the best way to access elements and attributes from a specific JSON file?

Is there a way to access each property within the JSON data provided? Additionally, I am interested in filtering specific objects based on their IDs. How can this be achieved using the array.filter method in JavaScript? { "records": [ ...

Guide on duplicating a Select2 element integrated with Django and Python

I am facing an issue where the select element inside a div is not cloning correctly. The Select2 element does not retain the loaded values from the Django code when cloned. Is there a way to clone the div with all the Select2 element values intact? Current ...

Vue modal fails to display when triggered by a specific query parameter

I've encountered an interesting challenge with my Vue.js app. Currently, the modal is displayed correctly when a button is clicked using the show() method: <script> export default { methods: { show() { console.log("showing modal ...

Focusing on a text field after reloading a different div with AJAX

I've been spending a lot of time figuring out the following issue and I'm hoping someone can help me find the solution. My web application has an input field (type="text") that is ready to accept user input when the page loads. When the user nav ...

What could be the reason for the base64 returned by url-loader in webpack appearing to be

After downloading and including the url-loader for Webpack, my configuration looks like this: loaders: [ { test: /.jsx?$/, include: path.join(__dirname, './public/scripts'), loader: 'babel-loader', exclude: /node_modul ...

Encountering issue 500 while attempting to establish a connection between MySql and PHP using mysqli

Here is the php code I wrote to establish a connection with MySQL database: <?php // Connect to MySQL $servername = "localhost"; $username = "user"; $password = "A11b22c33&"; // Create connection $conn = new mysqli($servername, $username, $pas ...

Utilize the keyboard's vertical arrows to adjust values as needed

Implement the functionality to increase and decrease the value in a label or p tags using the onkeydown and onkeyup events, without requiring a textbox input. I have come across numerous examples, but they all rely on textboxes. I am looking for a soluti ...

Display a Component in React that is similar to the one currently being rendered

Creating a feature in my React project where clicking on a card component (Card.js) opens another component (Suggestion.js) with matching IDs. Need help implementing the code for handling this functionality. Thank you! App.js // App component code here.. ...

Adding elements to a list using the appendChild method

Hey there! I have a bunch of images and I'm trying to create a navigation list item for each image. Here's the code I currently have: var bigImages = $('#imagesList li img'); // grabs the Big Images Next, I've set up a ul with t ...

Working with Changeable Variables in Node.js

Excuse me if this question seems foolish, but I'm struggling to find the answer. Context I'm currently working on an app using Node.js and the Express framework. I have a URI mapping set up to analyze Twitter data for sentiment on a specific su ...

Removing rows from a MySQL table that are present in another table

I am facing a situation where I have two tables, X and Y, each containing an E-Mail column (column names: EmailX in table X and EMailY in table Y). My goal is to remove the row(s) from table X if the email address in table X matches any of the email addres ...

Troubleshooting problems with resolving deeply nested promises

My approach to utilizing promises has been effective until now. The issue arises when the console.log(this.recipe) returns undefined and console.log(JSON.stringify(recipes)) displays an empty array. This suggests that the nested promises may not be resolvi ...

When you use Greasemonkey to click a link embedded within the content

In need of assistance with clicking a dynamic link using Greasemonkey. The only static part of the code is the text 'Attack This Player'. The href attribute of the link changes depending on the page. Here is my current code: function click_elem ...

The first argument in the Node.appendChild function does not adhere to the Node interface when trying to append to a new element

Hey there, I'm new to web development and could use some help. I'm encountering an error where I am trying to add an href attribute to an image tag. Here is my code: function linkus() { var a = document.getElementsByTagName("img"); ...