0

I have an array, which has a variable in it, like this:

let someStringVariable = "asdfljhasdlkjfhaasdfjasdfasd";

let string = ["1","36", someStringVariable, "bro" ];

I want to clone this array, but i want to get a new array with only values, without any variables in it

i've already tried slice(), but it seems like its not getting "someStringVariable" value as it is, its not coping it in the newArray.

What will you advise to try here?

  • 1
    The array `sting` *only* has values in it, not "variables". Because you *cannot* place a variable directly there, you get the value of it extracted. – VLAZ Oct 20 '20 at 17:17
  • 2
    It doesn't matter if `someStringVariable` is there or not--it will be the string either way – AlexH Oct 20 '20 at 17:17
  • Does this answer your question? [Javascript - replace words in string with object values](https://stackoverflow.com/questions/59469110/javascript-replace-words-in-string-with-object-values) – PM 77-1 Oct 20 '20 at 17:24

2 Answers2

0

Run the code below, this worked for me using your code:

let someStringVariable = "asdfljhasdlkjfhaasdfjasdfasd";

let string = ["1","36", someStringVariable, "bro" ]; 
console.log(string);

//check string array for someStringVariable
const stringWithoutVar = string.indexOf(someStringVariable);
//if someStringVariable exists in array then remove
if (stringWithoutVar > -1) {
  string.splice(stringWithoutVar, 1);
}

console.log(string);
Aib Syed
  • 3,118
  • 2
  • 19
  • 30
0

The variable isn't stored in the array. The value the variable points to on creation of the array is. To prove this I changed the variables value in the array. Notice that the variable in the array hasn't changed its value!

let someStringVariable = "asdfljhasdlkjfhaasdfjasdfasd";
let string = ["1","36", someStringVariable, "bro" ];
someStringVariable = 'Changed someStringVariable';
console.log( string )

So your array is already an array of values and not an array of values and variables. A nuance is that this is the case for primitive values ( strings, numbers, booleans, etc. ). With e.g. objects you keep reference, so changed properties will be affected.

A simple suggestion: Create an array with only strings and add the variables later and keep separate versions. E.g.:

let someStringVariable = "asdfljhasdlkjfhaasdfjasdfasd";

const base_array = ["1","36","bro"];
const variables_array = [ someStringVariable ]
let combined_array = [...base_array, ...variables_array ];
console.log( base_array );
console.log( variables_array );
console.log( combined_array );

Note: Whether this is the right solution for you depends on your specific scenario, which you didn't provide.

Rob Monhemius
  • 4,822
  • 2
  • 17
  • 49
  • Thank you very much for your answer! I'm totally new in js and programming itself, so (i hope, for a little period of time) i'm obviosly gonna make some mistakes, especially during many tries to describe some theory stuff )) I undesrtand that its not variable in array, more like 'link' to it , which placed outside the array. Main problem was that during test function, which had to check the array for the longest string in it, function didn't see the value of 'someStringVariable'. – Игорь Винцкевич Oct 21 '20 at 06:15