Skip to content
Advertisement

Tag: arrays

Javascript array sort and unique

I have a JavaScript array like this: I need the array elements to be unique and sorted: Even though the members of array look like integers, they’re not integers, since I have already converted each to be string: Is there any way to do all of these tasks in JavaScript? Answer This is actually very simple. It is much easier

JavaScript: Split array into individual variables

Considering this data structure: Looping through each vehicles item, is there a way to reassign the array elements to individual variables all in one shot, something like: … I’m trying to get away from doing: Just curious since this type of thing is available in other programming languages. Thanks! Answer Now it is possible using ES6’s Array Destructuring. As from

How to get character array from a string?

How do you convert a string to a character array in JavaScript? I’m thinking getting a string like “Hello world!” to the array [‘H’,’e’,’l’,’l’,’o’,’ ‘,’w’,’o’,’r’,’l’,’d’,’!’] Answer Note: This is not unicode compliant. “I💖U”.split(”) results in the 4 character array [“I”, “�”, “�”, “u”] which can lead to dangerous bugs. See answers below for safe alternatives. Just split it by an

How to declare an array in JS (like I’d do it in PHP)?

Hey, Im trying to make a nested array in JS Chrome’s debugger tells me the ), at the end of the first nested array is an “Unexpected Token” Answer From your code it looks like you’re trying to use PHP-style arrays in JavaScript. JavaScript arrays don’t work like PHP arrays. Here’s something that’s more likely to work: To explain a

Populating JavaScript Array from JSP List

Ok so perhaps someone can help me with a problem I’m trying to solve. Essentially I have a JSP page which gets a list of Country objects (from the method referenceData() from a Spring Portlet SimpleFormController, not entirely relevant but just mentioning in case it is). Each Country object has a Set of province objects and each province and country

Javascript array length incorrect on array of objects

Could someone explain this (strange) behavior? Why is the length in the first example 3 and not 2, and most importantly, why is the length in the second example 0? As long as the keys are numerical, length works. When they are not, length is 0. How can I get the correct length from the second example? Thank you. Answer

Advertisement