I have recently discovered a solution to a splicing issue I had where I could not access the value of the spliced object from an array of objects I have, it would show it as "undefined" when I tried to print the spliced object like this -
function QuoteSplicing() {
//splice quotes from the original array and push them into a new array.
spliceQuote = quotes.splice(quoteObject, 1);
quotes2.push(spliceQuote);
console.log(quotes);
console.log(quotes2);
if(quotes.length == 0){
quotes = quotes2;
quotes2 = [];
}
retu spliceQuote;
}
I then wrote a function that printed the spliced quote but it would show up as "undefined."
I fixed my QuoteSplicing function by adding an index value at the end of the splice statement -
function QuoteSplicing() {
//splice quotes from the original array and push them into a new array.
spliceQuote = quotes.splice(quoteObject, 1)[0];
quotes2.push(spliceQuote);
console.log(quotes);
console.log(quotes2);
if(quotes.length == 0){
quotes = quotes2;
quotes2 = [];
}
retu spliceQuote;
}
I have the fix after looking around, but I am trying to understand the why here as I feel it is important, how does adding the [0] index to the end cause the spliced quote to not show up as undefined anymore?
