Closed Bisstocuz closed 4 months ago
- Possible. But what's the point of it?
No another points, just I wanna know this: how to get thumbnail
's right index after calling IndexOrGet(5, "thumbnail")
I mean its practical meaning, as a reference to support or not
I mean its practical meaning, as a reference to support or not
Tip:
since Index() uses offset to locate data, which is much faster than scanning like Get()
So I guess SetByIndex()
should be much faster than Set()
, If I wanna change some fields from their original value, I hope to use index to set new value.
OK. Looks like it's worth doing that. Let me add a new API to support it -- or can you try to submit a PR for this?
OK. Looks like it's worth doing that. Let me add a new API to support it -- or can you try to submit a PR for this?
It looks like only one or two lines need to be changed to support this feature.
// IndexOrGet firstly use idx to index a value and check if its key matches
// If not, then use the key to search value
func (self *Node) IndexOrGet(idx int, key string) *Node {
if err := self.should(types.V_OBJECT, "an object"); err != nil {
return unwrapError(err)
}
pr := self.skipIndexPair(idx)
if pr != nil && pr.Key == key {
return &pr.Value
}
n, _ := self.skipKey(key)
return n
}
skipKey
returned the right index in the second returned value.
Should we directly add a return value to this method? Or is it better to create a new method to handle this logic? If we are to create a new method, what should the method name be?
A new method, to keep compatible with elder version. Give any name you like, just make it easy to understand
close via #594
Example data:
This is my
traveling
code:If use
ListIterator
, the copy of node will not change the source 'root' json content, onlyForEach
works but I only to particially update the keythumbnail
, no need to load another content.V_ARRAY
and edit its object's member?IndexOrGet
? For example, I wanna knowthumbnail
's index after callingIndexOrGet(5, "thumbnail")
.