首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何获取Object.keys(myEl)引用的子引用

如何获取Object.keys(myEl)引用的子引用
EN

Stack Overflow用户
提问于 2019-01-30 22:14:38
回答 2查看 1.4K关注 0票数 0

在下面的代码中,我可以获得对text000对象的引用,但我需要捕获它的子数组作为目标有效负载。一旦我有了钥匙的参考,我如何捕捉它的孩子?

完整的对象如下:

代码语言:javascript
运行
复制
activeItem = [{"dnd":{"index":0,"active":true,"group":"common","label":"Text (000)","type":"text"},
"json":{"schema":{"properties":{"text000":{"title":"Text (000)","type":"string"}},"required":["text000"]},"layout":[{"key":"text000","description":"","floatLabel":"auto","validationMessages":{"required":"Required"}}]}}]

要获取对"text000“键的引用,我使用:

代码语言:javascript
运行
复制
const myEl = Object.keys(this.activeItem.json.schema.properties); // points to text000

我需要提取该键的内容/子> {"title":"Text (000)","type":"string"} out,以便将其用作此操作的目标有效负载。

text000元素是动态的,因此我需要它的引用,这就是为什么我要使用Object.keys()方法来指向它。

请随便教我如何用正确的名字来指这些元素。例如,不确定如何引用键text000的> {"title":"Text (000)“、"type":"string"}。那是钥匙的“孩子”、“价值”、“内容”还是什么?

更新:

代码语言:javascript
运行
复制
console.log('TRY: ', this.activeItem.json.schema.properties[0]);
// Returns undefined

console.log('TRY2: ', this.activeItem.json.schema.properties);
// Returns {"text000":{"title":"Text (000)","type":"string"}}

我需要一些东西来回报:

代码语言:javascript
运行
复制
{"title":"Text (000)","type":"string"}

解决方案谢谢@jaredgorski:

代码语言:javascript
运行
复制
const properties = this.activeItem.json.schema.properties;

const propertiesKeys = Object.keys(properties);

const propertiesKeysFirstVal = Object.keys(properties)[0];

const logProperties = properties[propertiesKeysFirstVal];

console.log('PROPERTIES KEYS:', propertiesKeys);
console.log(
'VALUES OF FIRST PROPERTIES KEY:',
propertiesKeysFirstVal
);
console.log('RESULT:', logProperties);


PROPERTIES KEYS: ["text000"]
wrux-wrux-form-builder.js:1782 VALUES OF FIRST PROPERTIES KEY: text000
wrux-wrux-form-builder.js:1783 RESULT: {title: "Text (000)", type: "string"}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-30 22:23:08

您需要记住,activeItem是一个数组。只要包含索引(在本例中是第一个索引),就可以访问json属性(或键)并继续沿着链向下检索text000中的值。

这里的另一个技巧是,您希望访问properties中的第一个密钥,但您还不知道该密钥的名称。因此,您需要做的实际上是创建一个键数组,然后在那个properties对象中找出第一个键的名称。为此,您可以使用Object.keys(),一种将对象的键转换为数组的方法。一旦您有了这个键的名称,您只需要在properties对象上使用括号符号来找到这个键的值。我将在下面的片段中向您展示这是如何工作的。

以下是一些参考资料,以便您了解更多有关此操作的信息:

下面是一个有用的例子:

代码语言:javascript
运行
复制
const activeItem = [
  {
    "dnd": {
      "index": 0,
      "active": true,
      "group":"common",
      "label":"Text (000)",
      "type":"text",
      "icon":"text_fields",
      "fontSet":"material-icons",
      "class":""
    },
    "json": {
      "schema": {
        "properties": {
          "text000":{
            "title":"Text (000)",
            "type":"string"
          }
        },
        "required":["text000"]
      },
      "layout":[
        {
          "key":"text000",
          "description":"",
          "floatLabel":"auto",
          "validationMessages": {
            "required":"Required"
          }
        }
      ]
    }
  }
]

// This is the dirty looking version:

const logPropertiesDirty = activeItem[0].json.schema.properties[Object.keys(activeItem[0].json.schema.properties)[0]]

console.log("First, the dirty version where we don't save anything to variables. Everything is laid out here.")
console.log('WHAT WE DID:', 'activeItem[0].json.schema.properties[Object.keys(activeItem[0].json.schema.properties)[0]]')
console.log('RESULT:', logPropertiesDirty)


console.log('=================================================')

// This is the cleaner version, using variables to store things as we go:

const properties = activeItem[0].json.schema.properties;

const propertiesKeys = Object.keys(properties);

const propertiesKeysFirstVal = Object.keys(properties)[0];

const logPropertiesClean = properties[propertiesKeysFirstVal];

console.log('Now, the cleaner version. We save some values to variables to make things more readable.')
console.log('PROPERTIES OBJECT:', properties)
console.log('PROPERTIES KEYS:', propertiesKeys)
console.log('NAME OF FIRST PROPERTIES KEY:', propertiesKeysFirstVal)
console.log('RESULT:', logPropertiesClean)

关于如何称呼这些东西,我一直认为对象通常由“键值对”组成。键也可以称为属性,值也可以称为内容(我猜)。

代码语言:javascript
运行
复制
myObject = {
  key1: value1,
  property2: contentsOfProperty2
}

最后,清晰的沟通才是最重要的!所以,不管你想出什么名字(只要它们有合理的意义),我敢肯定,除非人们觉得有什么要证明的,否则他们不会对此感到厌烦。

票数 2
EN

Stack Overflow用户

发布于 2019-01-30 23:16:00

您应该能够使用Object.values over this.activeItem.json.schema.properties

Object.values()方法返回给定对象自身可枚举属性值的数组,其顺序与for...in循环提供的顺序相同(差异在于for-in循环也枚举原型链中的属性)。

代码语言:javascript
运行
复制
const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};
console.log(Object.values(object1));
// expected output: Array ["somestring", 42, false]

地图上还不支持它,但是如果需要的话,您应该能够加载聚脂填充

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54450383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档