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: { }
}
3
  • Why don't you want to create a new object? Commented Jun 26, 2020 at 17:30
  • You can use index and skip the starting two indexes Commented Jun 26, 2020 at 17:38
  • See this <div v-for="(item, index) in items"> <span>{{ index }}</span> </div> . Have a if condition and if index ===1 || index===2 dont do anything Commented Jun 26, 2020 at 17:40

3 Answers 3

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..

❤🌷😅

Sign up to request clarification or add additional context in comments.

Comments

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}]

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.