What is the best way to cut and combine multiple array elements in the right positions?

let result = response.data;
console.log(result);
const newTemp = result.ssps_with_scated.splice(5,1).concat(result.ps_with_ed.splice(2,1))[0];
result.ps_with_ed.push(newTemp); 

After multiple attempts, I finally achieved my goal. However, the array values may vary each time. Therefore, I've decided to reorganize them based on their IDs.

Answer №1

Greetings,

splice(7,1)(21,3)

This snippet of code is likely to trigger an error because Array.prototpy.slice generates a new array.
The outcome would be similar if you attempted the following:

const a = [1,2,3]
const b = a.splice(1,1);
b(2,1) // b.splice(...) is not a function

UPDATED: It's possible that there exists a more efficient or superior approach but... You can refine it for your specific scenario as follows:

const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21];
const first = array[7];
const second = array[21];

// Insert elements at certain positions in array (second and third)
array.splice(2, 0, first, second)

// Delete first from the array (index is 7 + 2 due to addition of 2 elements)
array.splice(9, 1)

// Exclude 21 from the array (index is 22 - 1 due to adding 2 elements and deleting 1, note: number 21 holds index 22)
array.splice(21, 1);

Answer №2

Information should not remain constant if it is being updated frequently. Remember, Splice function can only manipulate one array at a time. If multiple arrays need modification, consider using a loop or iteration over them.

Suppose you wish to insert the value from the 7th position into the 2nd position... you will have to execute something similar to this...

arr.splice(2, 0, arr[7])

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

Direct a flow to an unknown destination

What I am trying to achieve is writing a stream of data to nowhere without interrupting it. The following code snippet writes the data to a file, which maintains the connection while the stream is active. request .get(href) .on('response', func ...

How to import a JSON file in AngularJS using Node.js

I am currently working with NodeJS and AngularJS to handle some data. Within my Angular application, I am trying to access a JSON file and distribute its content into separate controllers. Right now, I am utilizing the http.get method within a controller ...

I am looking to fetch information from a different Firestore collection by looping through data using a forEach method within an onSnapshot function

I'm struggling to grasp the concept of rendering data from Firestore in my project. I've searched extensively but haven't been able to find a solution that fits my requirements. Background Information In my Firestore database, I have collec ...

Error: The current component does not have a template or render function specified. Check the <App>

I am a beginner in Vue js and I am facing an issue while running my application. It is showing an empty page with the error message: Component is missing template or render function. at <App>. Additionally, there is also a warning from Vue Router sa ...

Establishing a connection between a Google spreadsheet to create and automatically update calendar events

I'm currently working on connecting my Google Sheet to a calendar so that it can automatically generate calendar events and keep them updated based on changes made in the sheet. The Google Sheet I'm using tracks new building opening dates and con ...

Transfer information from a server to a client using the fetch API in pure JavaScript

Hey there, I've been working on a mini app that sends a fetch request to the backend and expects some information in return when a button is clicked. However, I'm facing an issue where the fetch call seems successful with a 200 status, but the d ...

Remove any JSON objects in JavaScript or AngularJS that match another JSON object

JSON A ['711','722','733','744'] JSON B [{pid: 711, name: 'hello'},{pid: 733, name: 'world'}, {pid: 713, name: 'hello'},{pid: 744, name: 'hellosdaf'}] I am attempting to remo ...

How can I troubleshoot email validation issues in Vue.js?

<button type="submit" class="register-button" :class="(isDisabled) ? '' : 'selected'" :disabled='isDisabled' v-on:click=" isFirstScreen ...

Attempting to design a universal form field component

While working on a web app with Vue, I found myself in need of creating multiple forms. To make this process easier, I started developing a generic input component that can be reused throughout the application. The plan was to be able to specify the number ...

Tips on sending data with a unique identifier to the backend through a route parameter

Can anyone help me figure out how to send a message to a specific backend route parameter while passing the current ID? I'm not sure how to inform the system about this ID. Here's the Vuex action: postMessage({commit}, payload, id) { axios.pos ...

How to create a Bootstrap panel that collapses on mobile devices and expands on desktop screens?

Is there a simple way to remove the "in" class from the div with id "panel-login" when the screen size is less than 1199px (Bootstrap's xs, sm, and md modes)? I believe JavaScript or JQuery could be used for this, but I'm not very proficient in e ...

Interactive tables created using Google Visualization

I have a Google visualization data table that works well, but I am facing an issue with the width of the table. When I click as shown in the image, I call the function drawTable(); and then I encounter this: As you can see, the table does not have a width ...

JavaScript pop-up that appears across all applications

An ASP.NET page I've developed in IE is responsible for monitoring multiple server jobs that run overnight. Whenever an error occurs on one of these jobs, a popup window opens using javascript's window.open() function. However, employees often ha ...

Gather information from every user and display their user ID in an HTML table, allowing for updates through a button

Can anyone help me create a table like the one shown here: The table should fetch data from my firebase database. Here is the structure of my requests database: . I've been trying to modify some code I found in a previous question. However, as you c ...

An issue arises when attempting to simultaneously utilize withDefaults and defineProps within the <script setup> block in a Vite + Vue project

I'm currently working on a project that involves using Vite + Vue. However, I've encountered an error in the code snippet below. Through my research, I discovered that the combination of withDefaults and defineProps within the <script setup> ...

Transitioning from using sendfile() to sendFile() function within the Express framework

Here is the code snippet I am using: router.get('/image',(req,res,next)=>{ const fileName = "path_to.jpg" res.sendfile(fileName,(err)=>{ if (err) { next(err); } else { console.log('Sent:', fileName); } ...

Is it possible to create a DOM element with a click handler all in one step?

I am looking to dynamically create an element, like this: var productItemTop = $( "<span>" + "<a class='spamItemsX' href='#' " + "onclick=" + eval(launchGenericProductSearch(topProducts)) ...

Tips for refreshing a DOM element after inserting with jQuery

I'm trying to update the LI element after using the insertBefore function in jQuery. The issue arises after adding new elements to UL where I encounter difficulty deleting the same element (using emailTagRemove). Check out the demo to see the proble ...

identifying the active input in vue.js

Below is an example where the value of the inputBox can be logged easily using two buttons. However, there is a third button whose task is to log the value of the inputBox that is currently in focus or active. How can this be achieved in vue.js? <div ...

Exploring the potential of $scope within $timeout in AngularJS

I am attempting to display a default message on a textarea using AngularJS. Some of the values I want to include require the use of $timeout to retrieve the values. The message does not appear to show up with the following code: <textarea class="t ...