Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8408093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:32:52+00:00 2026-06-09T23:32:52+00:00

I have an Object structured like this: { _id: { $oid: ‘foo’, }, data:

  • 0

I have an Object structured like this:

{
  _id: {
    $oid: 'foo',
  },
  data: {
    john: {
     _doe: {
       $oid: 'bar'
     }
    }
  }
}

Now I want to massage this object so that it becomes:

{
  _id: 'foo',
  data: {
    john: {
     _doe: 'bar'
    }
  }
}

In other words, every instance of $oid should move that value up one level (or something like that). It could occur at many levels.

I tried:

var loop = function(o) {
    for ( var i in o ) {
        if ( i == '$oid' ) {
            o = o[i];
        } else if ( typeof o[i] == 'object' ) {
            loop(o[i]);
        }
    }
    return o;
}
loop(obj);

But it fails, and I can’t get my head around it…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:32:54+00:00Added an answer on June 9, 2026 at 11:32 pm

    You almost have it, just needed a small fix:

    var loop = function(o) {
        for ( var i in o ) {
            if ( i == '$oid' ) {
                o = o[i];
            } else if ( typeof o[i] == 'object' ) {
                console.log(o[i]);
                o[i] = loop(o[i]);
            }
        }
        return o;
    }
    

    You need to assign the result of loop to the original object.

    o[i] = loop(o[i]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have the following structure: class foo(object): class bar(object): def __init__(self, parent): self._parent=parent #this
I have a document structured like this: { _id:43434, heroes : [ { nickname
I have a similar data structure like this: struct Data { std::string id; Blob
I have a JSON structure like this: { id:100 contactInfo: { name: John Doe
I have huge amont of geographic data represented in simple object structure consisting only
I have a tree-like object structure that consists of two types of objects: object
I have a NSDictionary like this one. PhotoData = { 1 = { jf_photo_geotag
I have a xml file structured like <?xml version=1.0?> <library> <book id=1003> <title>Jquery MVC</title>
I have some XML structures like this: var struct:XML = <mh> <mi id=1 stuff=whatever/>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.