Removing the final element within a nested array: a step-by-step guide

let originalArray=[
  [
    "Test1",
    "4",
    "160496"
  ],
  [
    "Test2",
    "6",
    "38355"
  ],
  [
    "Test3",
    "1",
    "1221781"
  ],
  [
    "Test4",
    "3",
    "124315"
  ],
  [
    "Test5",
    "5",
    "29509"
  ],
  [
    "Test6",
    "7",
    "47742"
  ],
  [
    "Test7",
    "2",
    "231034"
  ]
]

I am looking to extract only the first two elements from each sub-array in the original array. For example:

[["Test1","4"],["Test2","6"],["Test3","1"],["Test4","3"],["Test5","5"],["Test6","7"],["Test7","2"]]

Answer №1

let newArray=[
  [
    "Example1",
    "4",
    "160496"
  ],
  [
    "Example2",
    "6",
    "38355"
  ],
  [
    "Example3",
    "1",
    "1221781"
  ],
  [
    "Example4",
    "3",
    "124315"
  ],
  [
    "Example5",
    "5",
    "29509"
  ],
  [
    "Example6",
    "7",
    "47742"
  ],
  [
    "Example7",
    "2",
    "231034"
  ]
];

newArray.forEach(item=>
  (item.splice((item.length-1), 1))?item:false
);
 console.log(newArray);

Answer №2

Discover how to create a new array from an existing one using the array.map method

const array = [
  ["Example1", "7", "153560"],
  ["Example2", "9", "66217"],
  ["Example3", "5", "1239784"],
  ["Example4", "2", "91653"],
  ["Example5", "6", "18546"],
  ["Example6", "8", "35479"],
  ["Example7", "3", "210780"]
];
const newArray = array.map(item => [item[0], item[1]]);
console.log(newArray);

Answer №3

If you are looking to maintain the original array variable untouched and only remove the last index no matter how long each inner array is, the following solution could be of help:

let array=[
  [
    "Test1",
    "4",
    "160496"
  ],
  [
    "Test2",
    "6",
    "38355"
  ],
  [
    "Test3",
    "1",
    "1221781"
  ],
  [
    "Test4",
    "3",
    "124315"
  ],
  [
    "Test5",
    "5",
    "29509"
  ],
  [
    "Test6",
    "7",
    "47742"
  ],
  [
    "Test7",
    "2",
    "231034"
  ]
]

const newArray = array.map(innerArray => innerArray.slice(0, -1));

console.log(newArray);

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

Tips for determining the actions of a node prior to its inception

Is there a way to automatically run scripts on specific elements after their creation? For example, using a jQuery plugin like autoresize to expand the height of a textarea. If I use $('.textarea').autosize(), only the current textareas will be a ...

React component fails to display content following execution of Jquery Ajax request

Utilizing a basic jQuery ajax function to retrieve inline HTML code from an API $.ajax({ url: url, headers: { 'Accept': 'application/javascript' }, dataType: 'html', beforeSend: function(){ $('.load-mor ...

Using radio buttons to toggle the visibility of a div element within a WordPress website

I am currently working on creating a WordPress page using the custom page tool in the admin interface. My goal is to have 3 radio buttons, with 2 visible and 1 hidden. The hidden button should be automatically checked to display the correct div (although ...

Ways to resolve the issue with the Argument of type 'Date[]' not matching the parameter type '(prevState: undefined) in React

I've encountered an issue while trying to construct my project. The error message reads as follows: Argument of type 'Date[]' is not assignable to parameter of type '(prevState: undefined) Here's the excerpt of the code in questio ...

JS problem with using for and foreach loops in Node.js

I've been really stumped by this situation. Everything was running smoothly until 4 days ago when two of my cron daemon jobs suddenly stopped working. Instead of ignoring the issue, I decided to take the opportunity to rebuild and enhance the code. I ...

First Impressions of Datatables

Is there a way to display a specific range of rows with Datatables initially? For example, showing rows 100-105 only. Does anyone know if this is achievable using the options available? ...

What is the process for deleting an event in Vue?

My Vue instance currently includes the following code: async mounted () { document.addEventListener('paste', this.onPasteEvent) }, beforeDestroy () { document.removeEventListener('paste', this.onPasteEvent) }, methods: { onPasteEv ...

Having trouble installing Popper.js?

I have encountered an issue while attempting to install popper.js in my angular project. ng new <<project>> cd <<project>> npm install popper --save npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules&b ...

What methods can a Java application use to distinguish one browser from another?

Is there a way to determine if the browser being used is Firefox or Chrome? I am looking to create an application that will only run on a specific browser registered by a user. To achieve this, my application needs to be able to identify which browser the ...

Start the Express server by utilizing Grunt

Can anyone assist me with adding a task to my Gruntfile that will start my Express server instead of the default one? I attempted to create a task and use require("server.js"), but it doesn't seem to be working properly. When I run "grunt mytask", the ...

What is the best way to sort a combination of numbers and text strings?

Within my table, I have column names enclosed in <th> tags and only numerical values inside <td> tags. When the user clicks on a <th> tag, the sorting algorithm organizes the values in ascending or descending order. .sortElements(function ...

What is the best way to get the number of populated children in a Mongoose schema?

Hello all, I am just beginning to learn about MongoDB and Mongoose. Does anyone know of a simple method to count the answers for each topic after using the population method? var query = Topic.find({}); query.select("title answersRef"); query.populate({p ...

Tips for transferring data from a parent component to a child component in React?

Recently, I've been restructuring a small application that was initially confined to one file by breaking it into its own separate components. Right now, I have a child component called UsersTable which I am displaying within the parent component User ...

Instructions on dynamically positioning a collection of div elements at the center of a webpage container

I am looking to create a set of divs that are centered on the page and adjust in size according to the length of the username. .Container1 { display: table; width: 100%; padding:0; margin:0; -webkit-box-sizing: border-box; -moz-box-sizing: bor ...

Creating information with PhpExcelTemplator

I am encountering an issue when trying to generate the result enclosed in brackets [] using the export tool provided by alhimik1986/php-excel-templator. $sql = mysqli_query($conn, $qry); $result = mysqli_fetch_array($sql); foreach ($sql as $row) { $ ...

Pass the form data to the next page with javascript in HTML

While working on a website for a power plant, I encountered some issues that require assistance. The main problem is that our client does not want to set up a database on their server. This means I can only use html, javascript, and a bit of php. There is ...

Discover the secrets to acquiring cookies within a Next.js environment

I am currently working with Next.js and attempting to retrieve a cookie value. Below is the code I have written: import cookie from "js-cookie"; export default function Home({ token }) { return ( <div className="flex flex-col items ...

What is the best way to transfer variables to a different page through a pop-up window?

I'm working with a function that converts the Id of the clicked element into a variable, then opens a new window with a different page. How can I access or utilize this variable on the newly opened page? var idToWrite = []; $(function(){ $(".szl ...

Changing from a vertical to horizontal menu as the parent div is resized

I am looking for a solution to create a CSS effect or Javascript code that will hide a menu inside a div when the browser window or parent div is resized. I want to display another div with the same menu in horizontal orientation when the first one is hidd ...

ng-repeat and $scope problem

I am having an issue with my page where I display 3 images in a row using Ng-repeat. When I click on any image, it only shows the first image that was displayed in that particular row. Template: <div id="galscrolldiv" class="row" ng-repeat="image in i ...