I have an existing array to which i want to add an other array in the front of the existing array.
Add to the end is no problem with
[existingArray addObjectsFromArray:newArray];
But how to add it to the front?
To append one array to another, call the concat() method on the first array, passing it the second array as a parameter, e.g. const arr3 = arr1. concat(arr2) . The concat method will merge the two arrays and will return a new array. Copied!
var Array1 = ["Item 1", "Item 2"] var Array2 = ["Thing 1", "Thing 2"] var Array3 = Array1 + Array2 // Array 3 will just be them combined :) Save this answer.
If you are using List as an array, you can use its append(), insert(), and extend() functions. You can read more about it at Python add to List. If you are using array module, you can use the concatenation using the + operator, append(), insert(), and extend() functions to add elements to the array.
You can do this without a temporary array, and without assuming that newArray
is an NSMutableArray
, and without making an NSIndexSet
:
[existingArray replaceObjectsInRange:NSMakeRange(0,0)
withObjectsFromArray:newArray];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With