0

I am trying to learn how to cope with the objects and arrays and I saw many ways of iterating objects but recursing doesn't work for me and I don't understand why. What am I doing wrong?

I need to loop through an object and just slightly change something in an array. In my case, it's uppercasing the keys

Here is what I've got for now


const development = {
  port: 8080,
  db: {
    username: "jkdfs",
    password: "dsfsdg",
    name: "kslfjskd",
    test: { test: 12, another: 'value' }
  },
  token_secret: "jfkjdsj",
  hash_rounds: "kjsfkljdfkl"
};

function throughObject(obj) {
  let collection = {};
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      let value = obj[key];

      if (typeof obj[key] !== 'object') {
        collection[key.toUpperCase()] = value;
      } else {
        collection[key.toUpperCase()] = nestedObject(obj[key]);
      }
    }

    function nestedObject(nested) {
      const sub = {};

      for (const k in nested) {
        let v = nested[k];

        if (typeof nested[k] !== 'object') {
          sub[k.toUpperCase()] = v;
        } else {
          nestedObject(v);
        }
      }
      return sub;
    }
  }
  return collection;
}

const r = throughObject(development);
console.log(r);



7
  • sub[k.toUpperCase()] = nestedObject(v) ... Commented Dec 25, 2019 at 13:21
  • Also your inner and your outer function do exactly the same thing ... Commented Dec 25, 2019 at 13:21
  • 3
    You’re not doing anything with the recursive call result. Commented Dec 25, 2019 at 13:22
  • @JonasWilms, yes, I know that they are doing the same stuff. I've added the last one because my recursive didn't work. just was playing around with that Commented Dec 25, 2019 at 13:22
  • 1
    Your nestedObject() function is missing a return or an assignment when it calls itself recursively. Commented Dec 25, 2019 at 13:24

1 Answer 1

3

When you're recursively calling the function on an object value, you still need to assign it to the sub object: sub[k.toUpperCase()] = nestedObject(v). Also, you don't need 2 different functions.

const development = {
  port: 8080,
  db: {
    username: "jkdfs",
    password: "dsfsdg",
    name: "kslfjskd",
    test: { test: 12, another: 'value' }
  },
  token_secret: "jfkjdsj",
  hash_rounds: "kjsfkljdfkl"
};

function nestedObject(nested) {
  const sub = {};

  for (const k in nested) {
    const v = nested[k];
    
    if (typeof nested[k] !== 'object')
      sub[k.toUpperCase()] = v;
    else
      sub[k.toUpperCase()] = nestedObject(v); // <- HERE
  }
  return sub;
}

console.log(nestedObject(development))

Here's a shorter version using Object.fromEntries()

const development={port:8080,db:{username:"jkdfs",password:"dsfsdg",name:"kslfjskd",test:{test:12,another:"value"}},token_secret:"jfkjdsj",hash_rounds:"kjsfkljdfkl"};

const convert = o =>
  Object.fromEntries(
    Object.entries(o).map(([k, v]) => 
      [k.toUpperCase(), Object(v) === v ? convert(v) : v]
    )
 )

console.log(convert(development))

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

6 Comments

Thanks for your help a lot! But I already tried the first version of your code (not with Object.fromEntries) and it has the same result - only one level of objects is changed. Test stays undefined
and I tried to use for in loop because it is much more faster as far as I know. Comparing to Object.values, Object.keys
Yes, I can see now that it works in your snippet here but why it doesn't work in my IDE and I've copied and pasted it into codepan and I have an Empty object there?
@Valaryo check the browser's console. codepen's built-in only shows objects in the first level
That's why I already tried to one another function inside it :D but it helped only for one level
|

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.