0

I have an object of objects and I'd like to use a v-for loop to llop through all the objects except the first two ones, sadly I can't use slice sice it's only for arrays, is it possible to remove the first wo elements of an object using javascript without creating a new object

My object is something like:

{
    First: { },
    Second: { },
    Third: { }
}
gabogabans
  • 3,035
  • 5
  • 33
  • 81

3 Answers3

0

I am not that pro in js but first check this url

How to loop through a plain JavaScript object with the objects as members?

this just to get maybe an idea

How can I slice an object in Javascript?

so I will give you a logic where you may get a solution if you won't get answer from above when finished from url and get clear understand

create a function where it iterate over objects from what I suggest

then create a variable =1 if var_inc==1 or var-==2 continue else do whatever

then do a for loop to loop over over objects then do that function just get the logic maybe you get it..

paull
  • 15
  • 2
  • 7
0

JS objects don't store the order of elements like arrays. In the general case, there is no such thing as order of specific key-value pairs. However, you can iterate through object values using some utility libraries (like underscore https://underscorejs.org/#pairs), or you could just use raw js to do something this:

// this will convert your object to an array of values with arbitrary order
Object.keys(obj).map(key => obj[key])

// this will sort keys alphabetically
Object.keys(obj).sort().map(key => obj[key])

Note that some browsers can retain order of keys when calling Object.keys() but you should not rely on it, because it isn't guaranteed. I would suggest to just use array of objects to be sure of order like this:

[{ key: "First", value: 1 }, { key: "Second", value: 2}]
Amadare42
  • 415
  • 3
  • 14
0

If you just want to delete the properties of the objects then you can use delete keyword.

delete Obj['First']
delete Obj['Second']`

This would delete both the keys and object would have only 'Third' key

Madhur Bansal
  • 726
  • 2
  • 8
  • 14