How can I slice an object in javascript?

I tried to cut an object using Array.prototype, but it returns an empty array, is there any method for slicing objects other than passing arguments, or is it just my code that has something wrong? thanks!!

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); 
+6
source share
8 answers

I tried to Array.prototype an object using Array.prototype , but it returns an empty array

This is because it does not have a .length property. He will try to access it, get undefined , direct it to a number, get 0 and chop the maximum that many properties from the object. To achieve the desired result, you need to assign it length or an iterator through the object manually:

 var my_object = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four'}; my_object.length = 5; console.log(Array.prototype.slice.call(my_object, 4)); var sliced = []; for (var i=0; i<4; i++) sliced[i] = my_object[i]; console.log(sliced); 
+7
source

No one mentioned Object.entries () , which may be the most flexible way to do this. This method uses the same order as for..in when listing properties, i.e. The order in which the properties were originally entered into the object. You also get submarines with both property and value, so you can use any of them. Finally, you don’t have to worry about the properties being numerical or setting an additional length property (as with Array.prototype.slice.call() ).
Here is an example:

 const obj = {'prop1': 'foo', 'prop2': 'bar', 'prop3': 'baz', 'prop4': {'prop': 'buzz'}}; 

You want to cut the first two values:

 Object.entries(obj).slice(0,2).map(entry => entry[1]); //["foo", "bar"] 

Any clues?

 Object.entries(obj).slice(0).map(entry => entry[0]); //["prop1", "prop2", "prop3", "prop4"] 

Last key-value pair?

 Object.entries(obj).slice(-1) //[ ['prop4', {'prop': 'buzz'}] ] 
+10
source

I think this may help you:

 var my_object = { 0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four' }; var sliced = Object.keys(my_object).map(function(key) { return my_object[key] }).slice(4); console.log(sliced); 
+1
source

Try adding the 'length' property to my_object , and then your code should work:

 var my_object = { 0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', length: 5 }; var sliced = Array.prototype.slice.call(my_object, 4); console.log(sliced); 
+1
source

You cannot if it has no generator function [Symbol.iterator] and length . For instance:

 var my_object = { 0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', length:5 }, sliced; my_object[Symbol.iterator] = function* (){ var oks = Object.keys(this); for (var key of oks) yield this[key]; }; sliced = Array.prototype.slice.call(my_object, 2); console.log(sliced); 
0
source

You did not mention this in your question, but it looks awful like an object of arguments.

Convert it to an array using Array.from() , then use it like any other array. While this is an enumerated object.

For polyfill for older browsers see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from

0
source

 // Works, but acts weird when things get serious // Avoids screwing with the properties of my_object // You don't get Object.create() until ES5.1 function lazySlice(obj, idx) { var newObj = Object.create(obj, { length: {value: Object.keys(obj).length} }), idx = idx || 0; return Array.prototype.slice.call(newObj, idx); } // Only gives you own enumerable properties with keys "0" to n // Preserves element order (based on key number) // Ignores non-numbered keys // You don't get Object.keys() until ES5 function enumSlice(obj, idx) { var arr = [], keys = Object.keys(obj), idx = idx || 0; for (var i = 0; i <= keys.length - 1; i++) if (keys[i] >= idx && keys[i] % 1 === 0 && keys[i] >= 0 && keys[i].indexOf('e') < 0) arr.push(obj[keys[i]]); return arr; } var my_object = { 0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four' }; console.log(lazySlice(my_object, 3)); // [ 'three', 'four' ] console.log(enumSlice(my_object, 3)); // [ 'three', 'four' ] var mixed_object = { "9": 'nine', "2": 'two', "1": 'one', "7": 'seven', "7.5": 'seven point five', "1e4": 'sneaky', "-4": 'negative four', "0": 'zero', "length": 35 }; console.log(lazySlice(mixed_object)); // [ 'zero', 'one', 'two', , , , , 'seven', ] console.log(enumSlice(mixed_object)); // [ 'zero', 'one', 'two', 'seven', 'nine' ] 
0
source
 var obj = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four'}; var result = Object.keys(obj).slice(0,2).map(key => ({[key]:obj[key]})); console.log(result); 

[{'0': 'zero'}, {'1': 'one'}]

0
source

All Articles