All In One Script
MENU

How can I test if an array contains a certain value?

by 4:51:00 AM
undefined
How can I test if an array contains a certain value? 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? solution: Arrays.asList(yourArray).contains(yourValue) Warning: this doesn't work for arrays of primitives (see the comments). Since java-8 You can now use a Stream to check whether an array of int, double or long contains a value (by respectively using a IntStream, DoubleStream or LongStream) Example int[] a = {1,2,3,4}; boolean contains = IntStream.of(a).anyMatch(x -> x == 4); http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value

Create ArrayList from array

by 5:35:00 AM
undefined
Create ArrayList from array I have an array that is initialized like: Element[] array = {new Element(1), new Element(2), new Element(3)}; I would like to convert this array into an object of the ArrayList class. ArrayList<Element> arraylist = ???; Answer: new ArrayList<Element>(Arrays.asList(array)) http://stackoverflow.com/questions/157944/create-arraylist-from-array

What is the best way to add options to a select from an array with jQuery?

by 5:44:00 AM
undefined
What is the best way to add options to a select from an array with jQuery? What is the best method for adding options to a select from a JSON object using jQuery? I'm looking for something that I don't need a plugin to do, but would also be interested in the plugins that are out there. This is what I did: selectValues = { "1": "test 1", "2": "test 2" }; for (key in selectValues) { if (typeof (selectValues[key] == 'string') {

How to append something to an array?

by 5:02:00 AM
undefined
How to append something to an array? How do I append to an array in JavaScript? Solution: Use the push() function to append to an array: // initialize array var arr = [ "Hi", "Hello", "Bonjour" ]; // append new value to the array arr.push("Hola"); Now array is var arr = [ "Hi", "Hello", "Bonjour", "Hola" ]; // append multiple values to the array arr.push("Salut", "Hey"); Now array is var arr = [ "Hi", "Hello", "Bonjour", "Hola", "Salut", "Hey" ]; // display all values for (var

For-each over an array in JavaScript?

by 4:38:00 AM
undefined
For-each over an array in JavaScript? How can I loop through all the entries in an array using JavaScript? I thought it was something like this: forEach(instance in theArray) Where theArray is my array, but this seems to be incorrect. Answer: L;DR Don't use for-in unless you use it with safeguards or are at least aware of why it might bite you. Your best bets are usually a for-of loop (ES2015+ only), Array#forEach (spec | MDN) (or its relatives some and such) (ES5+ only), a simple old-fashioned for loop, or for-in with safeguards. But there's lots more to explore, read

Instagram