JSON Array Position Sequence

I have a system that takes input in the form of lines of text stored as an array, for example: array[123,556,"test",0,0]. By using val().split('\n'), I am able to add each new line to the array so that each line index is incremented by 1. Here's an example:

array[123,556,"test",0,1] = "line 1"
array[123,556,"test",0,2] = "line 2"
array[123,556,"test",0,3] = "line 3"
array[123,556,"test",0,4] = "line 4"

However, I need the last two indexes to display in reverse order. The arrays should look like this:

array[123,556,"test",1,0] = "line 1"
array[123,556,"test",2,0] = "line 2"
array[123,556,"test",3,0] = "line 3"
array[123,556,"test",4,0] = "line 4"

Somehow, they are able to increment the array index in the 4th position while I can only do it in the 5th position. I tried using .push(0) to add a 0 at the end but encountered errors.

Any suggestions or ideas on how to achieve this?

Thank you!

Answer №1

One approach is to cut out the element from the array and then append it back onto the end of the array.

let arr = [50,378,"example",2,9];
console.log(arr);  // [50, 378, "example", 2, 9] 

arr.push( arr.splice(3,1)[0] );
console.log(arr); // [50, 378, "example", 9, 2]

http://jsfiddle.net/2xbnfs7d/

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

Different methods for dynamically altering the style attribute

Here's some code I wrote: <html> <head> <style> .containerTitle { background-color:silver; text-align:center; font-family:'Segoe UI'; font-size:18px; font-weight:bold; height:30px; } ...

Rotate Chevron icon downwards on mobile devices in a Multilevel Dropdown Bootstrap 4

Bootstrap 4 is being utilized for my current project, I am looking to modify the rotation of the chevron icon in a multi-level dropdown menu specifically on mobile devices when the parent link is tapped, Here is the code snippet for the multi-level dropd ...

How can I execute a StoredProcedure using JavaScript?

I have a form with an email field and label <asp:TableRow runat="server"> <asp:TableCell runat="server"> <asp:TextBox runat="server" ID="txtUserEmail" onfocusout="emailVerification()" CssClass="forTe ...

Exploring the global document object within Next.js

We have been working on a project using nextjs and are facing an issue with changing the styling of a button upon click. Despite using document.getElementById, we are unable to change the styling no matter what we try. pages/index.js function Index() { ...

Why is it necessary to decode JSON stringified objects in Vue props?

When passing a stringifyed object via props to a component, it seems like there is an issue with the data transformation. <my-component :filter="stringobject" ></my-component> stringobject = "{"search_text":"(ciCl ...

Issue with ordering properties using @JsonPropertyOrder and jackson-module-jsonSchema

Using the latest branch for the Jackson module - jackson-module-jsonSchema version 2.4.4-Snapshot. Attempting to implement the @JsonPropertyOrder annotation to maintain the order of POJO attributes, but it seems that the annotation is not being respected. ...

Having trouble executing the .map function within the function

Context In the process of developing a React-Redux application, I am faced with the task of handling axios calls to an external API over which I have no control. The specific axios request in question is executed by a function called "getData". This reque ...

Add the teams-js library to your Vue.js project

I'm currently working on integrating the Microsoft Teams SDK into my Vue.js project. After installing the npm package and referencing it in my vue-file, I noticed that the microsoftTeams alias is displaying as undefined. Is there a step I may have ove ...

Tips for creating a fixed footer that adjusts with a flexible wrapper

Feeling incredibly stressed, I embarked on a quest to find the perfect sticky footer. After searching Google for what seemed like an eternity, I came across multiple tutorials recommending the use of min-height:100%. However, this approach caused the wrap ...

Is it possible to toggle between hide and show functions using Jquery?

I have a question about two jquery functions that I've been struggling with. $(document).ready(init); function init() { $(".alphaleft").hover(function (g) { $(".boxy,.yee").show(); }, function (g) { $(".boxy,.yee").hide(); }); } ...

VueJs Ellipsis Filter: Enhance Your Texts with

Using Vue.JS, I am dynamically injecting text content into a DOM element. <article>{{ movie.summary }}</article> My goal is to implement an auto-ellipsis filter. Essentially, the code would look like this: <article>{{ movie.summary | e ...

Angular 4 encounters performance issues when rendering numerous base64 images simultaneously

I'm currently working on a private project that involves using Angular 4 (specifically version 4.1.2). One issue we're facing is related to rendering multiple base64 images on an HTML page. The performance of the application significantly drops w ...

Utilizing AngularJS to Retrieve JSON Data in Controller using Factory

I am facing an issue with loading data in the controller, it is returning undefined. Below is my factory service: app.factory('factoryLlamada', function($http,$q) { return{ cargar: function(archivo) { var deferred = $q.defer(); ...

Maintaining aspect ratio of canvas while ensuring responsiveness

Currently, I am working on a drawing app and have come across an issue that has been challenging for me to resolve. The dilemma lies in resizing the canvas of sketches from the original resolution of 1280 x 720 to the maximum size possible upon opening the ...

Changing the key of a nested item within an array of objects using JavaScript

My task in JavaScript is to change the names of canBook -> quantity, variationsEN -> variations, and nested keys valueEN -> value. var prod = [{ price: 10, canBook: 1 }, { price: 11, canBook: 2, variationsEN: [{ valueE ...

The Jackson mapper outputs only the ID instead of the entire object

In my current project, I am employing Jackson 2.4.2 to map some Hibernate results. The issue I am encountering arises from the complexity of the Hibernate objects. Specifically, when dealing with a list of Hibernate objects, certain instances may referen ...

Encountering an unusual error while utilizing the Rails 3 form_for with the :remote option set to true

Encountering an error and seeking assistance: try { Element.update("status", "There seems to be an issue with this product."); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.update(\"status\", &bsol ...

Is it possible to create a translucent glass floating box over HTML elements, similar to the frosted glass effect seen in iOS7?

Creating an overlapping div with a frosted glass effect typically requires using an image background (like ). I am interested in having a floating div (position:fixed) that can apply a frosted glass effect over any content below it, whether it be an image ...

What is the best way to implement a delay in axios requests within a loop array?

I am currently working on a project in Vue where I need to add a delay to axios requests within a loop involving an array. let promises = []; for (const item of this.itemsWithIndex) { const cmd = "od_kioskPaperUpdate"; ...

Error encountered in jQueryUI Autocomplete: the function 'this.source' is not defined

I have been working on incorporating a live search feature that scans through keys in a JSON obtained from a public API. To achieve this, I am utilizing Jquery UI. However, I encountered the following error and I am uncertain about how to resolve it. Un ...