In CoffeeScript how do you append a value to an Array?
Good old push still works. x = [] x.push ‘a’
Good old push still works. x = [] x.push ‘a’
You can also create an associative array, or a “hash-table” like array, by specifying the index of the array. $array = array( 0 => array( ‘name’ => ‘John Doe’, ’email’ => ‘john@example.com’ ), 1 => array( ‘name’ => ‘Jane Doe’, ’email’ => ‘jane@example.com’ ), ); Which is equivalent to $array = array(); $array[0] = array(); … Read more
Convert without creating String object: import java.nio.CharBuffer; import java.nio.ByteBuffer; import java.util.Arrays; byte[] toBytes(char[] chars) { CharBuffer charBuffer = CharBuffer.wrap(chars); ByteBuffer byteBuffer = Charset.forName(“UTF-8”).encode(charBuffer); byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data return bytes; } Usage: char[] chars = {‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’}; byte[] bytes … Read more
Expand your sort function to be like this; function sortF(ob1,ob2) { if (ob1.strength > ob2.strength) { return 1; } else if (ob1.strength < ob2.strength) { return -1; } // Else go to the 2nd item if (ob1.name < ob2.name) { return -1; } else if (ob1.name > ob2.name) { return 1 } else { // … Read more
Try something like: row.map((rank, i, row) => { if (i + 1 === row.length) { // Last one. } else { // Not last one. } }) Old answer: const rowLen = row.length; row.map((rank, i) => { if (rowLen === i + 1) { // last one } else { // not last one } … Read more
array_unique works with an array of objects using SORT_REGULAR: class MyClass { public $prop; } $foo = new MyClass(); $foo->prop = ‘test1’; $bar = $foo; $bam = new MyClass(); $bam->prop = ‘test2’; $test = array($foo, $bar, $bam); print_r(array_unique($test, SORT_REGULAR)); Will print: Array ( [0] => MyClass Object ( [prop] => test1 ) [2] => MyClass … Read more
Javascript has a reverse() method that you can call in an array var a = [3,5,7,8]; a.reverse(); // 8 7 5 3 Not sure if that’s what you mean by ‘libraries you can’t use’, I’m guessing something to do with practice. If that’s the case, you can implement your own version of .reverse() function reverseArr(input) … Read more
You have an error in your syntax here: this._possessions = new Thing[100](); This doesn’t create an “array of things”. To create an array of things, you can simply use the array literal expression: this._possessions = []; Of the array constructor if you want to set the length: this._possessions = new Array(100); I have created a … Read more
If you are just checking for existence, HashSet<T> in .NET 3.5 is your best option – dictionary-like performance, but no key/value pair – just the values: HashSet<int> data = new HashSet<int>(); for (int i = 0; i < 1000000; i++) { data.Add(rand.Next(50000000)); } bool contains = data.Contains(1234567); // etc
a=(foo bar “foo 1” “bar two”) #create an array b=(“${a[@]}”) #copy the array in another one for value in “${b[@]}” ; do #print the new array echo “$value” done