Transform each array into individual objects

I am a beginner in javascript and I find arrays and objects to be a bit challenging. Currently, I am facing a hurdle with them.

Here is the array I'm working with:

[ '5', '13', '16', '22', '24' ] 1st <-- code for this

I am attempting to create a structure similar to the one below:

[null,{
            "0": 5,
            "1": "FFFRRR",
            "tx": 0,
            "ty": 0,
            "tz": 0,
            "rx": 0,
            "ry": 0,
            "rz": 0
        },
        {
            "0": 13,
            "1": "FFFRRR",
            "tx": 0,
            "ty": 0,
            "tz": 0,
            "rx": 0,
            "ry": 0,
            "rz": 0
        },
        {
            "0": 16,
            "1": "FFFRRR",
            "tx": 0,
            "ty": 0,
            "tz": 0,
            "rx": 0,
            "ry": 0,
            "rz": 0
        },
        {
            "0": 22,
            "1": "FFFRRR",
            "tx": 0,
            "ty": 0,
            "tz": 0,
            "rx": 0,
            "ry": 0,
            "rz": 0
        },
        {
            "0": 24,
            "1": "FFFRRR",
            "tx": 0,
            "ty": 0,
            "tz": 0,
            "rx": 0,
            "ry": 0,
            "rz": 0
        }]

Appreciate any help provided. Thank you.

Answer №1

When working with different codes, it is essential to assign each one to a corresponding object. Utilizing Array.prototype.map provides an efficient way to achieve this.

const codes = ['5', '13', '16', '22', '24'];

const mappedObjects = codes.map(code => {
    return {
        '0': Number(code),
        '1': 'FFFRRR',
        tx: 0,
        ty: 0,
        tz: 0,
        rx: 0,
        ry: 0,
        rz: 0,
    };
});

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

Rendering JSON Data in JavaScript using Table Pagination

Currently, I am working on rendering JSON data from a URL onto a table. My challenge is to display only 10 rows per page and I'm seeking guidance on how to achieve this. Below is the code snippet that I am using for rendering the data: const url = " ...

Transferring a selection from a dropdown menu

I am currently working on a dropdown menu where each entry has an ID and a value. The issue I am facing is that when I click on an item on the list, the click-handler is called and passes the div. While the click handler can access the id without any probl ...

Navigating PHP loops to input JSON data into MySQL database

I have a current PHP code that only grabs one row of data from dynamic JSON. I need it to loop through the changing JSON, which could contain 5 or 10 rows at different times. Despite trying various foreach() loops, none seem to work. $jsonCont = file_get ...

What could be causing the issues with SSL certificates when using Node.js/Express-TypeScript?

I'm currently in the process of transitioning a project's backend from JavaScript (Node.js/Express) to TypeScript. However, I've encountered an unusual issue where FS's readFileSync is unable to access the key.pem or cert.pem files in t ...

Prevent multer from uploading files if the field is left blank

Is there a way to prevent multer from attempting to upload a file if a specific field is left blank in a post request for users to update their profile? For example, if a user only wants to update their bio and not their profile picture. Here's the c ...

Transform a Vue JS Typescript object into an array and then back into an object through iteration

Having an object that contains an array of objects, I am looking to extract the data from each object in the array to show the opening hours. I have successfully accessed each key value within a for loop in the code snippet. However, I am unsure about how ...

What is the best way to simulate a click on an anchor tag using JavaScript?

In the setup of my website, there is a div container that includes an <input> element and an <a onclick="DoSomeStuff();" href="#" target="_blank"> element serving as a button. The function DoSomeStuff(); alters the href attribute based on the i ...

How can I take photos in bulk when I open the camera on Ionic 3?

Is there a way to capture multiple images at once using the camera? Currently, I am only able to capture one image when the user clicks. However, I would like to capture four images when the user clicks the success button. let options: CaptureImageOption ...

Common problems with handling foreign characters in Javascript JSON parsing

I am having an issue with parsing a valid JSON string containing Japanese characters. Every time I try to parse it, the process gets stuck on a specific character which is flagged as invalid. Below is the problematic string causing the error. Attempting t ...

Having trouble reaching the elements stored in an array?

As a beginner in Angular and JavaScript, I may be making some obvious mistakes so please bear with me. I have written this code that allows the selection of 2 text files and then combines them into a single array. $scope.textArray = []; $scope.textUpload ...

Personalized search feature for Bootstrap tables

Below is the table structure: <table> <tr> <th>Number</th> <th>Name</th> </tr> <tr> <td>200.10.1234</td> <td>Maria Anders</td> </tr> <tr> < ...

What is the best way to make a table row stand out when clicked on in a Vuejs application

How can I make a table row highlight when clicked in Vuejs using '@click'? I'm currently facing difficulties in achieving this. Below is my html template where I'm applying the class 'active' to the Boolean 'isActive&apo ...

Utilize a web service within a service file and leverage it across multiple locations

I am seeking help with reusing a service function in different parts of my app, specifically to display a certain parameter ('title') in the HTML template. Firstly, I created a service: import { Injectable } from '@angular/core'; impo ...

Tips on sending dynamic data with jQuery

I am struggling with titling this question properly, so I apologize for any confusion. My issue involves a JSON file containing two arrays. { "list 1": [ { "name": "label1", "imgPath": "images/list1img1.png" ...

Want to learn about Google Apps Script Web App? Join us to dive into AJAX, leverage

I'm attempting to follow the steps outlined in "Example 3: Web Response" on "this SparkFun tutorial" After implementing the code on script.google.com, I encountered an issue where I couldn't see the pin readings. Can someone provide assistance w ...

Steps for attaching distinct event listeners to individual elements:

I am currently developing a laravel project and I am faced with the task of iterating over a resource using the @foreach blade directive like this: @foreach($users as $user) <p @click="toggleClick">{{ $user->name }}</p> < ...

Creating Comet applications without the need for IFrames

Currently embarking on my journey to develop an AJAX application with server side push. My choice of tools includes Grizzly Comet on Glassfish V2. While exploring sample applications, I've noticed that most utilize IFrames for content updates on the c ...

Modify HTML button text after successful action

I need help with toggling the content of an HTML button (activate or deactivate) when clicked. I want the button to change from "Activate" to "Deactivate" and vice versa, using AJAX. Here is my code: <button type="button" class="btn btn-success btn-sm" ...

Is there a way to keep this function running endlessly when hovering?

Is there a way to modify this function so that the classes of my links continuously change colors while I hover over them, rather than changing only once? <script type="text/javascript"> $(".nav a").hover(function(e){ var randomC ...

Dynamically and asynchronously loading numerous LinkedIn share buttons on a page

On my page, I have a grid of post thumbnails that are fetched via AJAX and can be filtered. When a user clicks on a thumbnail, a carousel opens with the selected post centered. In this carousel, each post has a LinkedIn share button integrated. The issue ...