简体   繁体   English

获取对象键和父 javascript 的值并创建一个新对象

[英]get value of object key and parent javascript and create a new object

I have an object that looks like the following:我有一个如下所示的对象:

const testObject = {
  "NameA": {
    "Name": {
      "_text": "Peter"
    }
  },
  "AgeA": {
    "_comment": "line=2",
    "age": {
      "_text": "21"
    }
  },
  "Birth": {
    "_comment": "line=3",
    "Birthday": {
      "DateTimeSignUTCOffset": {
        "Date": {
          "_text": "191201"
        },
        "Time": {
          "_text": "1123"
        },
      },
      "Test": {
        "Code": {
          "_text": "1234"
        }
      },
    }
  }
}

I am trying to find any key with the key _text and get the corresponding value and the parent key.我正在尝试使用键_text查找任何键并获取相应的值和父键。

ie IE

const result = { 
    "Name": "Peter",
    "age": "21",
    "Date": "191201",
    "Time": "1123",
    "Code": "1234"
};

I have tried the following by looping through the object but am unable to figure it out.我通过遍历对象尝试了以下操作,但无法弄清楚。

const result = {};

const find_items = (object) => {
  console.log(Object.keys(object));
  Object.keys(object).map((item) => {
    console.log(object[item]);
    if(object[item] !== '_text') {
      find_items(object[item])
    } else {
      console.log(item)
    }
  });
};

find_items(testObject);

console.log(result);

Can someone could point me in the right direction?有人可以指出我正确的方向吗?

You could take a recursive approach and check for object and if _text property exist take the value with the outer key or get the entries from the recursive call with the object.您可以采用递归方法并检查对象,如果存在_text属性,则使用外键获取值,或者从使用对象的递归调用中获取条目。

At the end build an object from all entries.最后从所有条目构建一个对象。

 const flatEntries = object => Object .entries(object) .flatMap(([k, v]) => { if (v && typeof v === 'object') return '_text' in v ? [[k, v._text]] : flatEntries(v); return []; }); testObject = { NameA: { Name: { _text: "Peter" } }, AgeA: { _comment: "line=2", age: { _text: "21" } }, Birth: { _comment: "line=3", Birthday: { DateTimeSignUTCOffset: { Date: { _text: "191201" }, Time: { _text: "1123" } }, Test: { Code: { _text: "1234" } } } } }, result = Object.fromEntries(flatEntries(testObject)); console.log(result);

in English, what you want to do is:用英语,你想做的是:

create an empty object named "result", and then recursively iterate through the "object" object.创建一个名为“result”的空对象,然后递归遍历“object”对象。 each time you encounter a key linked to a sub-object which has a __text field, add that to the "result" object.每次遇到链接到具有 __text 字段的子对象的键时,将其添加到“结果”对象。

now just translate the above into JavaScript.现在只需将上面的内容翻译成 JavaScript。

the keyword here is "recursively".这里的关键字是“递归”。 your original code was not recursive.您的原始代码不是递归的。

your idea is pretty good, but you want to find _text keys anywhere nested inside the object.您的想法非常好,但是您想在嵌套在对象内的任何位置找到_text键。 To find inner, nested, keys, you need to recurse your function if the value of some key happens to be an object.要查找内部嵌套键,如果某个键的值恰好是一个对象,则需要递归函数。

result = {}

find_text_keys = (haystack, label) => {
  Object.keys(ob).forEach(key => {
    if (key === '_text') {
      res[text] = ob["_text"];
    } else if (typeof(ob[key]) === "object") {
      find_text_keys(ob[key], key);
    }
  });
}

Then, calling the function with a default label f(object, "default_label") will populate the result dictionary as you desired.然后,使用默认标签f(object, "default_label")调用函数将根据需要填充result字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM