issues with loop causing undefined values when using .push

I can't figure out why this particular piece of code is producing undefined values in the 'group' arrays:

Arr = [0,0,1,0,0,1,1,0,1,0]
for (j=0; j<5; j++){
    for (i in Arr) {
        this["groupS" + j + "C" + i] = [Arr[i]]
        for (a=1; a<=j; a++) {
            this["groupS" + j + "C" + i].unshift(Arr[i-a])
            this["groupS" + j + "C" + i].push(Arr[i+a])
        }
    }
}
console.log(groupS2C2)

It shows [0, 0, 1, undefined, undefined] for the group that's meant to include the first 5 elements of Arr. It appears that it works when using negative 'a' but not with positive 'a'.

Answer №1

Using for (i in Arr) is not suitable because Arr is actually an array, not an object. In order to loop through arrays, you should use an index like this:

for (i = 0; i < Arr.length; i++)

UPDATE: While technically Arr is considered an object, treating it as such may give partially correct results which might not be what you intended.

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

Implement a feature to dynamically load data as the user scrolls up, similar to the

I am in the process of creating a messaging platform and I am looking to implement a functionality where chat history is displayed upon scrolling up, similar to Facebook's chat system. Can anyone offer assistance with this task? ...

Countdown alert using JavaScript

Currently in my frontend code, I am utilizing angularjs as the javascript framework. In a specific section of my code, I need to display an error message followed by a warning message in this format: If a user inputs an incorrect month, then the following ...

When working in Javascript, make sure to replace newline and carriage return characters in strings with empty spaces. Also, don't forget to replace the literal sequences and

When working with Javascript, I am looking to perform multiple string replacements as outlined below. Remove all newlines and carriage returns Swap out instances of \n with a newline character Change occurrences of \r with a carriage return char ...

Is there a way to invoke a template literal tag function on a standard string?

Suppose I have a template literal tag function foo, which allows me to use it like this: const fooTaggedText = foo`some text`; Can I somehow invoke that tag on a normal string? For example: // This doesn't work as expected const fooTaggedText = foo ...

Vue.js data does not exhibit reactivity

I am encountering an issue with a non-reactive data object nested inside another object in my Vue.js template. Here is the code snippet: <template> <div> <b-container class="bg-white text-center scrollBehavior" > ...

Is there a way to display a success message once the button has been activated?

<template> <div> <div class="form-group"> <label for="name">Name</label> <input type="text" class="form-control" v-model="firstName" placeholder="Enter ...

Which should be used for front-end development: underscore.js or async.js with browserify?

For those in the back-end node.js development world, there's a fantastic library called async. If you're a front-end developer, you're probably familiar with the amazing library known as underscore. Interestingly, both of these libraries o ...

There seems to be a column in the MySQL INSERT query that is unidentifiable and does not exist in the list of fields

id and cost are required as integers and cannot be left empty. I encountered the following error message: 'Error: ER_BAD_FIELD_ERROR: Unknown column 'undefined' in 'field list'' var sql = "INSERT INTO Paintings(` ...

Looking to add elements to a specific div dynamically using jQuery? Let's explore how to insert comments seamlessly

I would like to implement a comment system that adds entered comments to a specific div. Here's the code I have so far: <ul class="comments"> <li> <a class="commenter_name" href="/">Dushyanth Lion</a> ...

Learn the process of showcasing database content on a webpage with an interactive feature that enables users to choose and access additional details

Apologies if this question has been asked before, I have searched for a solution but my web development knowledge is limited. To better understand my issue, you can visit the site at 000freewebhost by following this link: In summary, I am trying to select ...

Is it feasible to solely utilize the "else" block for an "if...else" statement in JavaScript?

Is it possible to have an "else" block without an "if" statement in JavaScript? If so, what is the syntax for this situation? if (req.body[data].length <= maxlength[indx]); // <===== semicolon else { req.body[data] = 'ERROR: too lo ...

Exploring the integration of jquery Datatables within a vue.js framework

As a newcomer to Vue, I've been experimenting with integrating Vue with jquery datatables for a project requirement. However, I have come across some issues while trying to combine the two. Despite my efforts in the code snippets below, I have not bee ...

utilize javascript variables within an HTML document

I keep encountering a strange error (Express 400 Error: Bad Request) Some lines are translated to the variable value, while others just output an error. This is an example of my code: exports.add_comment = function(req, res){ var id = req.params.id; ...

In ReactJS, organizing arrays within arrays for nesting purposes

Recently, I initiated a fresh project with React. I designed and brought in images by importing them: import Tip1 from "./img/Tips1.png"; import Tip2 from "./img/Tips2.png"; import Tip22 from "./img/Tips2-2.png"; ...

Execute the function upon clicking

When I click on an icon, I want it to blink. This is the code in my typescript file: onBlink() { this.action = true; setTimeout(() => { this.action = false; }, 1000) return this.action }; Here is how the action is declared in my ...

Storing new li and ul elements to the database by utilizing an array for dynamic saving

I am in the process of creating a user interface for an application where users can add unordered lists and list items by clicking on "add question". I have successfully designed the front end part and here is a preview: However, I am facing difficulties ...

Switching JButton Icon when Clicked

I am working on developing a Java JFrame Application that resembles Minesweeper but with some unique rules and objectives. Currently, I have set up a grid of JButtons in a 12x12 layout using a JButton 2D array. My goal is to change the image displayed on ...

How do I retrieve an index value from an array in GraphQL?

I am seeking a solution for obtaining the index of an array item that is not returned by downstream response. Let's consider my schema: schema: type Cart { items: [Item] } type Item { name: String index: Int } When the data comes in, it lo ...

When the loop is malfunctioning due to a certain condition

what happens if the condition in the controller file is not executed during runtime... ...

Issues encountered when using Three.js raycasting to position objects above a plane

My heightmap-based plane is not working with raycasting, and I'm also having trouble placing an object above the hovered triangle. UPDATE: By commenting out the height section (pgeo.vertices[i].z = heightmap[i] * 5;), it seems to work inconsistently. ...