var arr=[1,2,3,[4,5],6,[7,8,9]],x,j;
for(x in arr)
for(j in arr[x])
console.log(arr[x][j]);
The desired output should be 1, 2, 3,..., 9. However, the current code generates 4, 5, 7, 8, 9.
var arr=[1,2,3,[4,5],6,[7,8,9]],x,j;
for(x in arr)
for(j in arr[x])
console.log(arr[x][j]);
The desired output should be 1, 2, 3,..., 9. However, the current code generates 4, 5, 7, 8, 9.
In my opinion, simply using the "join" method should suffice:
var result = arr.join();
console.log(result);
If I have correctly understood your inquiry, you are seeking to console log numbers 1 through 9. The current setup will only output the arrays within your array, explaining why you are receiving 4, 5, 7, 8, and 9.
One approach is to verify if the value is an array in your initial loop - if it is, iterate through it and display the values. If not, simply print the value.
if(arr[x].constructor === Array) {
//loop over the array and print out the values
for (j in arr[x]) {
console.log(arr[x][j])
}
} else {
//print out the plain value
console.log(arr[x])
}
You can view the results in this codepen: http://codepen.io/kyledodge/pen/zGwPBo
Alternatively, recursion can be used. A sample implementation could look like this:
var printArrayValue = function(array) {
for (var i = 0; i < array.length; i++) {
if (array[i].constructor === Array) {
//if this an array, call this function again with the value
printArrayValue(array[i]);
} else {
//print the value
console.log(array[i]);
}
}
}
printArrayValue(arr);
View the outcome in this codepen: http://codepen.io/kyledodge/pen/VLbrPX
Convert each element into an array:
let myArray = [10, 20, 30, [40, 50], 60, [70, 80, 90]], index;
for(index in myArray) {
let newArray = [].concat(myArray[index]);
^^^^^^^^^^^^^^^^^
for(let j in newArray)
console.log(newArray[j]);
}
This method is effective because concat
can handle both arrays and single values.
My query is about creating an expandable tree structure while iterating through an array in AngularJS. I managed to make it work, but the issue is that all nodes expand and collapse together. Here's my HTML: [...] <div ng-repeat="item in items"&g ...
Below are the necessary form data that needs to be posted using an AJAX request in order to receive a JSON response. <textarea type='text' id="newStatusBox">Your Status here...</textarea> Link:<input type="text" id="newStatusLink" ...
Currently, I am utilizing Django as a backend and attempting to pass some data into a Vue table component that I have created. I followed this informative tutorial to set it up. The Vue component displays correctly when using webpack. My approach involves ...
While there have been many inquiries regarding how to create a ping command for a discord.js bot, my question stands out because I am attempting to develop this command for interaction rather than message. I attempted utilizing Date.now() - interaction.cre ...
Looking at the object structure in Chrome Dev Tools, it appears like this: obj: { 1: {...}, 2: {...}, 3: {...}, 4: {...}, 5: {...}, } On the other hand, there is a simple array as well: arr: [1,3,5,7] The goal here is to filter the object bas ...
I'm facing a challenge while integrating Leaflet with React, where Leaflet seems to want control over the DOM rendering as well based on my research. Currently, I have countries being properly colored according to specific color codes derived from ba ...
Currently, I am in the process of developing an HTML5 App through Intel XDK. On my initial page (page_0), users have the ability to choose a conversion style. Opting for the first option directs them to page_1 where they encounter input boxes and buttons. ...
Every time I attempt to utilize a code snippet like the one below: jQuery.post("http://mywebsite.com/", { array-key: "hello" }); An error message pops up saying: Uncaught SyntaxError: Unexpected token - I have experimented with adding quotation m ...
My Node-express code currently uses module.exports to export functions. As I am converting the code to TypeScript, I need to find out how to replace module.exports in typescript. Can you help me with this? ...
After initiating mongodb on my computer, I noticed that the log is being stored at /usr/local/var/log/mongodb/mongo.log. Each time I execute a query/insert/delete/update, it also gets recorded in this file. I have attempted to suppress these messa ...
I am currently working on making dynamically created divs draggable. While I have successfully achieved this with pre-existing div elements as shown below: <div> This can be dragged around, but outputs cannot?! </div> the issue arises when ...
One option to consider is always jQuery. I am in search of a lightbox that provides a "full screen" effect. Not necessarily filling the entire screen, but rather covering most of the content on the page. The lightboxes I have come across either only displ ...
For my current project, I am attempting to create a visible spotlight similar to the one used by Batman. I want that cone of light that pierces through the night sky. Unfortunately, I do not have any experience with graphics or 3D design, so I am strugglin ...
Attempting to create a responsive page with two distinct sections at this example link including: Map View Table View Both of these views (table and map divs) need to be responsive without a hard-coded height, so the size of the map div adjusts automatic ...
For my current project, I am utilizing angular2-infinite-scroll. My concept is to load 6 items on the initial page load and then add an additional 6 items each time the user scrolls to the bottom of the page. However, I have encountered an issue where the ...
I am attempting to extract specific information from a large spreadsheet containing data. The spreadsheet consists of 2 columns, labeled Email and Reference. I conducted tests using the provided sample: Email Reference --------------------- ...
After receiving a POST response from an HTML form in my PHP script, I encounter a text that contains a list of items separated by commas (e.g., "apple, banana, orange"). I intend to store this text along with other inputs received through POST into a tabl ...
I have successfully utilized the code snippet below to generate an excel file in node.js. My intention is for this generated file to be downloadable automatically when a user clicks on a designated download button. var fs = require('fs'); var w ...
Currently, I am working on using ajax to trigger the execution of a Python file that continuously monitors changes in a text file. If any changes are detected, it will communicate back to ajax for further actions. The Python script must start running as so ...
My Pagination Code: http://plnkr.co/edit/WmC6zjD5srtYHKopLm7m This is a brief overview of my code: var app = angular.module('hCms', []); app.controller('samplecontoller', function ($scope, $http) { $scope.showData = function( ...