Fastest way to duplicate an array in JavaScript – slice vs. ‘for’ loop

There are at least 6 (!) ways to clone an array: loop slice Array.from() concat spread operator (FASTEST) map A.map(function(e){return e;}); There has been a huuuge BENCHMARKS thread, providing following information: for blink browsers slice() is the fastest method, concat() is a bit slower, and while loop is 2.4x slower. for other browsers while loop … Read more

Remove Object from Array using JavaScript

You can use several methods to remove item(s) from an Array: //1 someArray.shift(); // first element removed //2 someArray = someArray.slice(1); // first element removed //3 someArray.splice(0, 1); // first element removed //4 someArray.pop(); // last element removed //5 someArray = someArray.slice(0, someArray.length – 1); // last element removed //6 someArray.length = someArray.length – 1; … Read more

Selecting last element in JavaScript array [duplicate]

How to access last element of an array It looks like that: var my_array = /* some array here */; var last_element = my_array[my_array.length – 1]; Which in your case looks like this: var array1 = loc[‘f096012e-2497-485d-8adb-7ec0b9352c52’]; var last_element = array1[array1.length – 1]; or, in longer version, without creating new variables: loc[‘f096012e-2497-485d-8adb-7ec0b9352c52’][loc[‘f096012e-2497-485d-8adb-7ec0b9352c52’].length – 1]; How … Read more

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5] len(my_list) # 5 The same works for tuples: my_tuple = (1,2,3,4,5) len(my_tuple) # 5 And strings, which are really just arrays of characters: my_string = ‘hello world’ len(my_string) # 11 It was intentionally done this way so that lists, tuples and other container types or iterables didn’t all need to explicitly implement … Read more

How to remove all duplicates from an array of objects?

How about with some es6 magic? obj.arr = obj.arr.filter((value, index, self) => index === self.findIndex((t) => ( t.place === value.place && t.name === value.name )) ) Reference URL A more generic solution would be: const uniqueArray = obj.arr.filter((value, index) => { const _value = JSON.stringify(value); return index === obj.arr.findIndex(obj => { return JSON.stringify(obj) === _value; … Read more

Split a String into an array in Swift?

Just call componentsSeparatedByString method on your fullName import Foundation var fullName: String = “First Last” let fullNameArr = fullName.componentsSeparatedByString(” “) var firstName: String = fullNameArr[0] var lastName: String = fullNameArr[1] Update for Swift 3+ import Foundation let fullName = “First Last” let fullNameArr = fullName.components(separatedBy: ” “) let name = fullNameArr[0] let surname = fullNameArr[1]

Check if an array contains any element of another array in JavaScript

Vanilla JS ES2016: const found = arr1.some(r=> arr2.includes(r)) ES6: const found = arr1.some(r=> arr2.indexOf(r) >= 0) How it works some(..) checks each element of the array against a test function and returns true if any element of the array passes the test function, otherwise, it returns false. indexOf(..) >= 0 and includes(..) both return true … Read more

How do I store an array in localStorage? [duplicate]

localStorage only supports strings. Use JSON.stringify() and JSON.parse(). var names = []; names[0] = prompt(“New member name?”); localStorage.setItem(“names”, JSON.stringify(names)); //… var storedNames = JSON.parse(localStorage.getItem(“names”)); You can also use direct access to set/get item: localstorage.names = JSON.stringify(names); var storedNames = JSON.parse(localStorage.names);

How to convert an Array to a Set in Java

Like this: Set<T> mySet = new HashSet<>(Arrays.asList(someArray)); In Java 9+, if unmodifiable set is ok: Set<T> mySet = Set.of(someArray); In Java 10+, the generic type parameter can be inferred from the arrays component type: var mySet = Set.of(someArray); Be careful Set.of throws IllegalArgumentException – if there are any duplicate elements in someArray. See more details: … Read more