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?

3
  • 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. Commented Oct 20, 2020 at 17:17
  • 2
    It doesn't matter if someStringVariable is there or not--it will be the string either way Commented Oct 20, 2020 at 17:17
  • Does this answer your question? Javascript - replace words in string with object values Commented Oct 20, 2020 at 17:24

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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'.
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.