Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

javascript - Understand the recurring function

Welcome,

Question about correct answer from topic: Get all keys of a deep object in Javascript

I don't understand how this "getDeepKeys" function works. I know that there are a lot of threads related to this topic, but they don't give me the full answer to my question.

The details of my question are:

  1. piece of code on which I am testing the function:

var data2 = {
  OrderMessage:{
    EntityId: "ZZ-KR/00000002/2020/1",
    Buyer1: {
      GLN: "GLN.6340134346_Buyer_1",
    },
    Seller1: {
      GLN: "GLN.6340134346_Seller_1",
    },
  },
  OrderResponseMessage: {
    Standard: "STANDARD_NAME",
    Buyer2: {
      GLN: "GLN.6340134346"
    },
    Seller2: {
       GLN: "GLN.7690933887",
    },
  }
}
function getDeepKeys(obj) {
  var keys = [];
  for(var key in obj) {
      keys.push(key);
      if(typeof obj[key] === "object") {
          var subkeys = getDeepKeys(obj[key]);
          keys = keys.concat(subkeys.map(function(subkey) {
              return key + "." + subkey; 
          }));
      }
  }
  return keys;
}

var objectt = getDeepKeys(data2);
console.log(objectt);
question from:https://stackoverflow.com/questions/65560251/understand-the-recurring-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From your question my understanding is that you are confused how the different iteration of the getDeepKeys() method knows to which keys variable it should save the value.

then after calling the function, an empty key array is created again keys

Yes it creates another variable named keys. But unlike we human computer identify each keys by their address in the memory. You can declare as many variable with same name as you want in different iteration of the method. They will be stored in different memory location in your PC. That is why the getDeepKeys() method never confuses to which keys it should save the array.

Lets say for the first iteration when you create the keys array it is being stored in the memory location 10AD32 and for the 2nd iteration when you create another keys array it will be stored in a different memory location such as 11CD34. Therefore although both variable have name keys but computer can identify them differently.

So for each iteration you will have a different keys variable.

For better understanding you can read this, this and this.

Note that when describing the array storing procedure I intentionally over simplified it for you. The whole array will not stored in a single memory location. In reality the array name refers to the position where the array head is stored.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...