Remove the 5th element from the given array, starting with the element at index 2

My array consists of 3 groups, with each group containing 5 elements.

var tempArray1 = ['prodn', 'PP1', 'UK1', 'Exp', 'India2', 'prodn', 'PP2', 'france1', 'Imp', 'Czech2', 'prodn', 'PP3', 'Germ1', 'Exp', 'Rom2']

I am looking to remove the 2nd element in my array and then delete every 5th element, which will eliminate all elements starting with "PP". It is important for me to remove these elements based on their position in the array, not by character type. The following code demonstrates how I plan to remove every 5th element:

var indexToRemove = 5;  // start position
var numberToRemove = 1; // elements to remove

tempArray1.splice(indexToRemove, numberToRemove);

However, I am unsure of how to initiate this process from the 2nd element. Any guidance would be appreciated. Thank you.

Answer №1

When the splice method is used, it directly modifies the array (in-place). Therefore, when removing a value from the array, you should increment by 4 instead of 5 for subsequent steps. To implement this, initiate a for loop starting at index 1:

const tempArray1 = ['prodn', 'PP1', 'UK1', 'Exp', 'India2', 'prodn', 'PP2', 'france1', 'Imp', 'Czech2', 'prodn', 'PP3', 'Germ1', 'Exp', 'Rom2'];

for (let i = 1; i < tempArray1.length; i += 4) {
    tempArray1.splice(i, 1);
}

console.log(tempArray1);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Answer №2

    let elementsToRemove = ['prodn', 'PP1', 'UK1', 'Exp', 'India2', 'prodn', 'PP2', 'france1', 'Imp', 'Czech2', 'prodn', 'PP3', 'Germ1', 'Exp', 'Rom2']
    for(let i=2-1; i< elementsToRemove.length; i+=5-1){
        // start with 2nd element (-1 because arrays start at 0)
        // and then jump to 5th element (-1 because the one has just been removed)
        // remove 1 element at i-th place 
        elementsToRemove.splice(i,1);
    }

Answer №3

let exampleArray = ['A1', 'B2', 'C3', 'D4', 'E5', 'F6', 'G7'];

const modifiedArray = exampleArray.filter((item, index) => {
    return (index % 3) != 0;
});

console.log(modifiedArray);

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

Identifying whether a Property in an Object is another Object

I'm planning to utilize JSON for stringifying the value of a property within an object, which will then be stored as a Tag on Google Calendar using Google Apps Script. The value in question is actually a double-nested Object (look at extraAttributes w ...

Convert the value to JSON format by utilizing the PHP GET method

After retrieving a value using the GET method in my PHP file, I am attempting to access it. Below is how my PHP file appears: <?php include 'Con.php'; header('content-Type: application/json'); $catid = $_GET["CatId"]; //array ...

Exploring shader file content within three.js with the help of jQuery

I am trying to retrieve the content of a string that I have imported using HTML from within another script. To include the text file in question in the html file: <script src="shaders/fragmentshader.fs" id=fragmentshader></script> After impo ...

The error message at line 757 in utils.ts states that the [div] element is not recognized as a valid <Route> component

I've been trying to set up routes for Register and Login components inside a div with the className 'container' using react-router v6. However, whenever I try to access these components, I end up on a blank page. Strangely, when I remove the ...

When attempting to pass an AJAX JavaScript variable to a Django view, an AttributeError is encountered stating, "The 'WSGIRequest' object does not have an attribute 'data'."

Attempting to pass the JavaScript variable this.click from my .js file to my Django view class Click through Ajax is resulting in the following error message... AttributeError: 'WSGIRequest' object has no attribute 'data' This is my v ...

Is there a way to identify the index of user input when using the .map method?

I'm currently utilizing the Array.prototype.map() method to present an array within a table. Each row in this table includes both an input field and a submit button that corresponds to each element from the Array.prototype.map() function. Is there a w ...

JavaScript time zone error specific to Internet Explorer

In my code, I am attempting to retrieve the timezone name or ID based on the client's local time. When testing with clients from the United States, I correctly receive EDT. However, when trying the same code with clients in the Indian timezone, it ret ...

What is the best way to make the current year the default selection in my Select control within Reactive Forms?

Hey there! I managed to create a select element that displays the current year, 5 years from the past, and 3 years from the future. But now I need to figure out how to make the current year the default selection using Reactive Forms. Any ideas on how to ac ...

Unable to retrieve dynamically generated object property from an array in AngularJS 2+

Here is an example of an items array: this.itemList = [ { id: 1, name: 'a', address: 'as dasf a' }, { id: 2, name: 'b', address: 'as dasf a' }, { id: 3, name: 'c', address: 'as dasf a' } ]; ...

Guide to embedding a qr code within a pdf document

I've been working on creating a PDF file generation feature where users can download the PDF with a QR code embedded in it. While I've successfully implemented the PDF generation part, I'm facing an issue with adding the QR code to the file. ...

Changes in date format using jQuery datepicker

I am having trouble with the code below. Whenever I attempt to change the date, it switches from 15/5/2012 to 05/15/2012. <script> $(document).ready(function(){ $("#date").datepicker({ }); var myDate = new Date(); var month = myDa ...

Encountering an issue while trying to verify user registration in MySQL using Node.js Express. During the user registration process, an error arises indicating that 'res' is not defined

import React from 'react' import { Link } from 'react-router-dom' import {useNavigate} from 'react-router-dom'; import { useState } from 'react' axios from "axios" const Register = () => { const [i ...

Move a pin on Mapbox

I have a question regarding my map markers: https://i.sstatic.net/2HndV.png Currently, the center of the blue markers align with my desired coordinates. Is there a way to adjust the markers slightly upwards so that the tip sits exactly on my specified po ...

The server was unable to start because NPM was unable to navigate to the wrong directory and locate the package.json file

I recently used vue create to start a project and everything went smoothly. However, when I tried to run npm run serve, I encountered an issue where node couldn't locate the package.json file that was generated during the project creation process. Th ...

Chrome-exclusive: Dealing with Overflow in Twitter Bootstrap Form Inputs

I've encountered a strange issue where datetime-local inputs exceed their boundaries on Chrome (tested on Chrome 35 x64 for Linux), but not on Firefox (29.0). Here's a screenshot demonstrating the problem in Chrome: chrome screenshot And here&a ...

Creating a custom drag and drop page builder from the ground up

My objective is to create a custom tool for dynamically adding HTML elements to a blank webpage and designing a web page layout. I am aware that this can be achieved using jQuery's drag-and-drop functionality, but I am struggling with organizing the e ...

Implement a horizontal scrollbar for your table in HTML

I need to implement a horizontal scroll bar for my table. Here is the HTML code: Even though I have utilized Bootstrap, the horizontal scroll bar is not working for this particular module. It works fine in other modules. The tbody data is added after an ...

How come the font size and div elements combine when I drag and drop the items?

Recently, I decided to create my own drag and drop game. The game is almost complete, but there's one issue. When I try to drop the items into the designated "Drop Items Here" area, their style changes abruptly to mimic the text. For example: https: ...

Stryker score caused the Jenkins build to fail

Is there a way to configure the Jenkins pipeline so that it fails when the stryker score is below X? This is the stryker configuration: config.set({ mutator: "javascript", mutate: [...], testRunner: "jest", jest: { projectType: "n ...

Effortless navigation fails to glide seamlessly

I have scoured through numerous resources on achieving smooth scrolling, but I am still unable to make it work. I am considering writing the code in a "js/script.js" file and then pasting the link to "script.js" at the bottom of the main page. Do I need to ...