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

Resolve React Issue: Using Functions as React Children is Invalid

As I follow along with a React tutorial, I encountered an error while adding a POST request: Functions are not valid as a React child. This may happen if you return a Component instead of from render. Or maybe you meant to call this function rather than ...

Struggling to eliminate the scrollbar on a Material UI Dialog

I have a modal window that includes a keyboard, but I'm encountering some issues. Despite adding overflow:'hidden' as inline CSS, the scrollbar refuses to disappear. Furthermore, even when utilizing container-full padding-0 in Bootstrap, th ...

What is the best way to selectively pass certain values to the args object?

Is there a way in TypeScript to pass only one argument into args and have other values be default without using "args = {}" or declaring defaults within the function to avoid issues with intellisense? function generateBrickPattern ( wallWidth: number, ...

Finding the index of a nested div element with a click event using jQuery

I'm currently working on a click event to retrieve the index of the outermost parent div, but I'm facing some difficulties in getting it to work. Here is a snippet showcasing a list of several divs: <div class="owl-item"> <div class= ...

Executing Code Upon Module Load and Presenting It as Middleware

I am currently delving into the intricacies of how exporting and importing modules function in Nodejs. One of my tasks involves using a specific file to seed a mongodb database. Surprisingly, this file operates flawlessly and delivers the desired results ...

The code functions properly when tested on a local server, but doesn't produce any results when

Currently, I am working on customizing a premade website and have been tasked with making a modification. The goal is to implement a confirmation box that appears when a button to delete a meeting task is clicked, rather than immediately deleting it. Afte ...

Generating dynamic strings for identifiers in JSX

Can you help me figure out how to dynamically create IDs like this? <div id="track_1"></div> <div id="track_2"></div> I tried assigning the IDs from the parent component like this: export default function Compon ...

Image uploads are a challenge for Angular

I'm facing an issue with my Angular app while trying to upload an image to the server (node backend). Despite trying various methods found online, I have encountered the same problem consistently - the server does not receive the image. Interestingly, ...

Is it possible to conceal JavaScript comments on a page before it is displayed?

Curiosity has led me to ponder a question that may seem trivial. Consider this scenario: in my HTML or ASPX page, I include a comment for some JavaScript code. When the page loads, will the comments be rendered along with the rest of the page's conten ...

How can I prevent ajax loader from overlapping headers in jquery-datatables?

Currently, I am developing a web application that utilizes jQuery data tables to load data from the server side. One issue I have encountered is that when the data table first loads, the loader covers the headers, creating a visibility problem as depicted ...

Creating nested objects from nested arrays in PHP is a simple task that involves iterating through the

Here is a code snippet that initializes a config: $this->config = array( 'users' => array( array('name' => 'admin', 'password' => $password ) ...

Exploring the power of V-for with checkboxes in VueJS

One issue I am facing is related to triggering my save method only when a checkbox is selected or true in Vue JS. I have no problem listing values and saving them to the database using axios, Vue JS, and Spring Boot. However, every time I click the checkbo ...

Breaking apart field values in React Final Form

In my react and redux application, I am using react final form with a 'cars' field that is a select. When the submit event is triggered, it should only return specific types like coupe: ['BMW'] instead of just the field name with values ...

"Endless loop conundrum in ngrx router

Currently, I am facing an issue while trying to integrate ngrx router-store into my application. After referencing the example app on GitHub, it seems that adding a `routerReducer` property to my application state and including the `routerReducer` from ngr ...

Turn off the background graphic in chartjs

Greetings all! I'm currently facing an issue with removing the hashtag background of a line chart. Is there any method to disable or remove it? I am utilizing chartjs in my Ionic 2 app. Your assistance is greatly appreciated. Below is the code for my ...

Using React with Typescript: Anticipating child component with particular props

I'm currently developing a component that necessitates the use of two specific child components. These two components are exported using dot notations from the main component and have defaultProps for identification within the main component: export ...

"Is There a Specific Order for Organizing Angular Router

Could you please clarify the difference between these two code snippets? const routes: Routes = [ { path: '', canActivate: [AuthGuard], component: MainComponent, children: [ { path: '', component: DashboardComponent }, ...

The function $$.generatePoint is not recognized in the Billboard.js library

Having some trouble with integrating billboard.js into my Vue project as an alternative to using d3.js. Struggling to get it working in both my repository and a vanilla Vue project. Anyone familiar with the process of getting billboard.js running smoothly ...

The datepicker function is malfunctioning in a div that has been dynamically loaded through

I am experiencing an issue with loading a div populated by ajax. The datepicker does not seem to be called when the content is loaded this way. Initially, I had the datepicker loaded in the header of the main page, but even after trying to load it within ...

Implement an interface with a specific number of properties in TypeScript

I am attempting to create a custom type that defines an object with a specific number of key-value pairs, where both the key and value are required to be numbers. Here is what I envision: type MatchResult = { [key: number]: number; [key: number]: numbe ...