Skip to main content

Questions tagged [arrays]

An array is an ordered linear data structure consisting of a collection of elements (values, variables, or references), each identified by one or more indexes. When asking about specific variants of arrays, use these related tags instead: [vector], [arraylist], [matrix]. When using this tag, in a question that is specific to a programming language, tag the question with the programming language being used.

11983 votes
150 answers
12.8m views

How can I remove a specific item from an array in JavaScript?

How do I remove a specific value from an array? Something like: array.remove(value); Constraints: I have to use core JavaScript. Frameworks are not allowed.
Walker's user avatar
  • 133k
5722 votes
41 answers
5.4m views

Loop (for each) over an array in JavaScript

How can I loop through all the entries in an array using JavaScript?
Dante1986's user avatar
  • 59.4k
4949 votes
62 answers
3.6m views

How do I check if an array includes a value in JavaScript?

What is the most concise and efficient way to find out if a JavaScript array contains a value? This is the only way I know to do it: function contains(a, obj) { for (var i = 0; i < a.length; i++...
brad's user avatar
  • 75k
4276 votes
34 answers
3.5m views

How to insert an item into an array at a specific index?

I am looking for a JavaScript array insert method, in the style of: arr.insert(index, item) Preferably in jQuery, but any JavaScript implementation will do at this point.
tags2k's user avatar
  • 83.8k
4159 votes
61 answers
3.4m views

Sort array of objects by string property value

I have an array of JavaScript objects: var objs = [ { first_nom: 'Laszlo', last_nom: 'Jamf' }, { first_nom: 'Pig', last_nom: 'Bodine' }, { first_nom: 'Pirate', last_nom: '...
Tyrone Slothrop's user avatar
4107 votes
42 answers
1.9m views

Create ArrayList from array

Element[] array = {new Element(1), new Element(2), new Element(3)}; How do I convert the above variable of type Element[] into a variable of type ArrayList<Element>? ArrayList<Element> ...
Ron Tuffin's user avatar
  • 54.4k
4081 votes
33 answers
3.7m views

Checking if a key exists in a JavaScript object?

How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist, and I try to access it, will it return false? Or throw an error?
Adam Ernst's user avatar
  • 53.6k
4040 votes
46 answers
5.4m views

Loop through an array in JavaScript

In Java, you can use a for loop to traverse objects in an array as follows: String[] myStringArray = {"Hello", "World"}; for (String s : myStringArray) { // Do something } Can ...
Mark Szymanski's user avatar
3333 votes
51 answers
2.1m views

How can I check if an object is an array? [duplicate]

I'm trying to write a function that either accepts a list of strings, or a single string. If it's a string, then I want to convert it to an array with just the one item so I can loop over it without ...
mpen's user avatar
  • 280k
3073 votes
27 answers
3.8m views

Deleting an element from an array in PHP

Is there an easy way to delete an element from an array using PHP, such that foreach ($array) no longer includes that element? I thought that setting it to null would do it, but apparently it does ...
Ben's user avatar
  • 68.1k
2889 votes
30 answers
5.4m views

How to append something to an array?

How do I append an object (such as a string or number) to an array in JavaScript?
2863 votes
94 answers
3.5m views

Get all unique values in a JavaScript array (remove duplicates) [duplicate]

I have an array of numbers that I need to make sure are unique. I found the code snippet below on the internet and it works great until the array has a zero in it. I found this other script here on ...
Mottie's user avatar
  • 86k
2728 votes
32 answers
2.7m views

How do I determine whether an array contains a particular value in Java?

I have a String[] with values like so: public static final String[] VALUES = new String[] {"AB","BC","CD","AE"}; Given String s, is there a good way of testing whether VALUES contains s?
Mike Sickler's user avatar
  • 34.2k
2552 votes
31 answers
5.8m views

How do I declare and initialize an array in Java?

How do I declare and initialize an array in Java?
bestattendance's user avatar

15 30 50 per page
1
2 3 4 5
27852