I was trying to slice an object using Array.prototype, but it returns an empty array, is there any method to slice objects besides passing arguments or is just my code that has something wrong? Thx!!
var my_object = {
0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four'
};
var sliced = Array.prototype.slice.call(my_object, 4);
console.log(sliced);
The best modern solution to this is the combination of Object.fromEntries and Object.entries.
You can't unless it has a
[Symbol.iterator]
generator function andlength
property exists. Such as;You don't mention it in your question, but that looks awfully a lot like an arguments object.
Convert it to an array using
Array.from()
then use it like any other array. As long as it is an enumerable object.For a polyfill for older browsers, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
I think it can helps you: