1

I am trying to isolate the first value from an array that is constructed like below using JS:

[john,mike,tom]

I have tried the slice method but it is not working and I am assuming it's the way the array is constructed where the strings aren't enclosed in quotes. What would be the best way to transform the array above to either a string as a whole or a more properly formatted array so I can pull out any value I want?

edit For additional context, the array I mentioned above is the way is being passed to me from the source. I am trying to figure out how I can work with it to be able to slice up the values.

Sorry for the layman presentation of my question but I am still quite the beginner in JS and would appreciate it if anyone could help me out. Thanks!

4
  • you need some variables with the names, you have in the array. to get a value, take a property accessor. Commented May 15, 2018 at 20:28
  • You cannot make an array without the quotes unless john and mike are other variables... Commented May 15, 2018 at 20:30
  • Take a look how the slice method works, there are useful examples regards what you need to achieve developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented May 15, 2018 at 20:32
  • Sorry, I should've been more clear but with the array example I mentioned, that is how the "value" is being passed to me so I am trying to figure out how I can work with it to be able to isolate the values. Is there a way to correct the construction of the array so that the names are strings instead of variables? Commented May 15, 2018 at 20:50

3 Answers 3

4

In javascript, strings are enclosed in quotes. e.g.

'john', "mike" ect. so in order to create an array/list you need to put these values with quotes inside array and then access using index e.g.

var array = ['john', 'mike', 'tom']
console.log(array[0]); // john
Sign up to request clarification or add additional context in comments.

Comments

0

Why do you need the strings to not have quotes? Is there an specific reason?

In js you need to put quotes on strings. If you try for example declaring an array in the way you did above the following will happen:

let x = [janaina,joao,julia]

VM203:1 Uncaught ReferenceError: janaina is not defined at :1:10

So, correct way to delcare your array:

let x = ['janaina','joao','julia']

Now slice will work:

x.slice(0,1);

The result will be:

['janaina']

1 Comment

Hi, sorry didn't mention it but that is actually that the way the "value" is being passed on to me that I am trying to work with. Is there a way to convert that to a properly constructed array with string values instead?
0

You can use the array prototype .shift(), but it will mutate your original array.

const john = {x: 124};
const mike = {x: 333};
const tom = {y: 355};

const slicy = [john, mike, tom].shift();

console.log(slicy);

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.