var arr= ['g','o','o','d'];
var arr2 = arr.join();
Arr2 will be "g,o,o,d". I would like to get "good". I know there are a million other ways to achieve this but was curious if there was a way with join.
Sure - just pass an empty string:
var arr2 = arr.join('');
Related
I've searched through the answers here, but I can only find this question answered for other languages.
So I have 2 Uint8 typed arrays.
var arr1 = [0,0,0];
var arr2 = [0,1,2,3,4,5,6,7,8,9];
I want to replace the contents of arr2 with arr1 starting at the 4th position. So that arr2 will be:
arr2 = [0,1,2,0,0,0,6,7,8,9];
If I wasn't trying to do this in the middle of the array I could use set like this:
arr2.set(arr1);
And I would get:
arr2 = [0,0,0,4,5,6,7,8,9];
I know I can loop through the arr2 and individually copy the values, but performance wise this is very slow compared to set (and performance matters to me because it's copying an entire array of canvas img data 24 times a second).
Is there any function that can copy into the middle of an array, but with the performance of set?
Use the typedarray.set(array[, offset]) offset.
offset Optional
The offset into the target array at which to begin
writing values from the source array. If you omit this value, 0 is
assumed (that is, the source array will overwrite values in the target
array starting at index 0).
const arr1 = new Uint8Array([0,0,0]);
const arr2 = new Uint8Array([0,1,2,3,4,5,6,7,8,9]);
arr2.set(arr1, 4);
console.log(arr2);
You can use the slice method with the spread syntax:
const shim = (source, index, target) => [
...source.slice(0, index),
...target,
...source.slice(index)
]
var arr1 = [0,0,0];
var arr2 = [0,1,2,3,4,5,6,7,8,9];
const newArr = shim(arr2, 3, arr1);
console.log(newArr);
.slice will not mutate the array and will return a new shallow copy of it (unlike splice).
Since you are using typed array. Don't you can use the offset of the set method?
arr2.set(arr1, 3)
To overwrite from the 4th element of the target array.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set
To me it does just what you need, if I understand your question.
I have
var myArray = ["word", "are", "tame"];
var result = myArray.join('s').split('a');
1)computed: "wordsarestame"
2)computed: "words", "rest", "me"
My question is for the first computed,
computed: "wordsarestame"
why don't you add a 's' at the end of me? since it's .join('s') on whole myArray
You're joining 3 things, so you get 2 s in between. It's the same as doing:
var joined = "word" + "s" + "are" + "s" + "tame"
Array.join joins the elements of the array - it means it puts a glue element between each two array elements in succession. That's why you don't have it neither at the beginning of joined string, nor at the end of it.
Join combines the array into a string, and uses the provided string as the glue that holds them together. You don't need to glue anything at the end of the last string, so no more glue is applied:
var arr = [1, 2, 3];
console.log(arr.join('x'));
You could use map() to get your desired result:
var arr = [1, 2, 3];
console.log(arr.map(str => str + 'x').join(''));
Array.prototype.join()
manual
The join() method joins all elements of an array (or an array-like
object) into a string.
It glue each element of array elements. If you want to add a after theme, add another '' element of the array, then ues join().
var myArray = ["word", "are", "tame"];
myArray.push('');
console.log(myArray.join('s'));
I have an array and I'm using join("") to turn it to the string, but when I console.log it one letter is under the other. How can I change that to make letters next to eachother?
var array = [d,o,g];
var array2 = array.join("");
console.log(array2);
result:
d
o
g
and I want:
dog
It works this way, you have.
var array = ['d','o','g'];
var array2 = array.join("");
console.log(array2);
Put letters inside quote :
var array = ['d','o','g'];
var array2 = array.join("");
console.log(array2);
I am trying to make a copy of a javascript array and modify the 'copy' without modifying the original variable too, can anyone explain what I am doing wrong..
e.g
var array1 = [2, 5];
var array2 = '';
array2 = array1;
array2.pop();
console.log(array1);
console.log(array2);
// both output the same, however I want it to show array1 with 1 single item
I am trying to make it so array2 will only contain the one item in the array & array1 will contain two items in the array. Any ideas what I'm doing wrong?
In order to create a copy of an array rather than assign the reference value you can use the .slice method.
array2 = array1.slice(0);
Slice returns a (shallow) copy of the given array from the given index to the (optional end index)
Use slice() to copy the array.
var array1 = [2, 5];
var array2 = '';
array2 = array1.slice(0);
array2.pop();
console.log(array1);
console.log(array2);
slice does not alter the original array, but returns a new "one level
deep" copy that contains copies of the elements sliced from the
original array.
Documentation
It is as simple as using the slice function:
var array1 = [2, 5];
var array2 = '';
array2 = array1.slice();
array2.pop();
console.log(array1);
console.log(array2);
Here you are telling javascript to get a copy of the elements in array1 and assign the result to array2. By not setting the start and ending position it will slice the whole array.
When you writing this
array2 = array1 //It creates reference on array1 object
array2.pop() // removes 5 from both end
So you have to clone one array to another by slice() method---
array2 = array1.slice(0) // cloning array1 's element to array2
array2.pop() // removes 5 from only array2 not from array1
Now it works fine
Alter the line array2 = array1; to:
array2 = array1.slice(0);
Slice method returns a (shallow) copy of the given array from the given index to the (optional end index)
I hope this helps.
var array1 = [column1,column2,column3,column4];
var array2 = [column1 [empid],column2 [mobno],column4 [place]];
if array1 has any of array2 values. I mean, in above case column1,column2,column4 is there in array1, then remove those values and array1 should have only column3 and then append array1 value with array2.
after check and remove, now array1 = [column3] then append array1 value with array2 and finally
array2 = [column1 [empid],column2 [mobno],column3,column4 [place]]; it should be in correct index position
is above case possible?
I would recommend using a library like Underscore.js, it has functions like _.union() that would do what you described. If you don't want to the library you could have a look at their implementation.
Using your example:
array2 = _.union(array1, array2);
if you dont want to use any library then you can do
for(elem in array2){
var index = array1.indexOf(array2[elem]);
if(index > -1)
array1.splice(index,1);
}
array2 = array1.concat(array2).sort();