I have an array that i'd like to remove the first index of an array before. I've looked up some examples and saw that .shift()
is the quickest and easiest method to use in what I am trying to accomplish.
so I declare a variable stateData
to equal the original array.shift
my console.log log is still returning the first index of my array. Alternatively I tried array.slice(1)
but when console logging stateData
I am getting back an empty array.
It does not seem that my syntax is off with either of these methods so I am not sure why this is not working.
I am expecting to have the first index of the array removed.
Here is my array:
arr:
Array(10) [ "States", "207,390", "205,020", "160,502", "110,073", "62,681", "35,069", "43,716", "45,471", "58,549"]
My expected outcome is have the return array display without having "States" in index zero. Only return the string of numbers.
Here is my code:
First attempt:
var arr = ["States", "207,390", "205,020", "160,502", "110,073", "62,681", "35,069", "43,716", "45,471", "58,549"];
function drawGraph(arr) {
let stateData = arr.shift()
console.log(stateData, "stateData") // Array(10) [ "States", "207,390", "205,020", "160,502", "110,073", "62,681", "35,069", "43,716", "45,471", "58,549"]
}
drawGraph(arr);
Second Attempt:
var arr = ["States", "207,390", "205,020", "160,502", "110,073", "62,681", "35,069", "43,716", "45,471", "58,549"];
function drawGraph(arr) {
let stateData = arr.slice(1)
console.log(stateData, "stateData") // console.log returns Array(0) []
}
drawGraph(arr);
console.log()
statements are in your code and what they're logging? Even better would be a runnable StackSnippetshift
: "Theshift()
method removes the first element from an array and returns that removed element."arr.shift()
removes and remove the first element. So when you dolet stateData = arr.shift();
the variablearr
is modified and nowstateData
store the removed element.