jquery's map doesn't return native array, you need to use get()
slideHeights = $('*[data-anchor]').map(function(i, item) {
return Math.floor($(item).offset().top);
}).get();
Or use toArray
slideHeights = $('*[data-anchor]').map(function(i, item) {
return Math.floor($(item).offset().top);
}).toArray();
Answer from gurvinder372 on Stack Overflowjquery's map doesn't return native array, you need to use get()
slideHeights = $('*[data-anchor]').map(function(i, item) {
return Math.floor($(item).offset().top);
}).get();
Or use toArray
slideHeights = $('*[data-anchor]').map(function(i, item) {
return Math.floor($(item).offset().top);
}).toArray();
If anyone got this error who are new to JavaScript, make sure your spelling is right.
Array.unshift()does not follow camelCase.
So it would be Array.unshift() instead of Array.unShift()
You've added an element to "_array", and its index will be 0. However, you're trying to use an array as an index into an array, and that won't work. That is,
_array[u]
doesn't make sense. Arrays are indexed numerically. Thus the value of that expression is undefined, and that explains the error message — you're trying to reference the "unshift" property of the value undefined. (Well, undefined isn't really a value, but whatever.)
use
_array[0].unshift();
instead of
_array[u].unshift('somevalue');
You need to call the function addFlavor as well:
let originalFlavors = [...];
function addFlavor(originalFlavors) {
originalFlavors.unshift("Rainbow Sherbert");
}
addFlavor(originalFlavors);
console.log(originalFlavors);
Make sure you're defining the array, and then calling the function:
let originalFlavors = []
function addFlavor(of) { // use a different name to avoid shadowing and confusion
of.unshift("Rainbow Sherbert");
return of;
}
originalFlavors = addFlavor(originalFlavors);
console.log(originalFlavors);
Try
var arr = Array.prototype.slice.call(allMessages);
arr.unshift(data);
You can see how does Array.prototype.slice.call() work?
I can think of this work around;
var tempMessage= {};
tempMessage.data = data;
for(var key in allMessages){
tempMessage[key] = allMessages[key];
}
Than just set it back
allMessages = tempMessage;
method 'unshift' does not return modified array. you can see how it works here.
and mutating state without setState method is not recommend because in that case React cannot detect the change.
you can use setTodos(arr => [newTodo, ...arr]) instead.
you can also try this as the array 'unshift' method does not return a modified array.
Clone the state first using the spread operator and then update the state.
function addItem(newTodo) {
let arrayToUpdate = [...todos];
arrayToUpdate.unshift(newTodo);
setTodos(arrayToUpdate);
}