在下面的代码中,我可以获得对text000对象的引用,但我需要捕获它的子数组作为目标有效负载。一旦我有了钥匙的参考,我如何捕捉它的孩子?
完整的对象如下:
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“键的引用,我使用:
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"}。那是钥匙的“孩子”、“价值”、“内容”还是什么?
更新:
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"}}我需要一些东西来回报:
{"title":"Text (000)","type":"string"}解决方案谢谢@jaredgorski:
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"}发布于 2019-01-30 22:23:08
您需要记住,activeItem是一个数组。只要包含索引(在本例中是第一个索引),就可以访问json属性(或键)并继续沿着链向下检索text000中的值。
这里的另一个技巧是,您希望访问properties中的第一个密钥,但您还不知道该密钥的名称。因此,您需要做的实际上是创建一个键数组,然后在那个properties对象中找出第一个键的名称。为此,您可以使用Object.keys(),一种将对象的键转换为数组的方法。一旦您有了这个键的名称,您只需要在properties对象上使用括号符号来找到这个键的值。我将在下面的片段中向您展示这是如何工作的。
以下是一些参考资料,以便您了解更多有关此操作的信息:
下面是一个有用的例子:
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)
关于如何称呼这些东西,我一直认为对象通常由“键值对”组成。键也可以称为属性,值也可以称为内容(我猜)。
myObject = {
key1: value1,
property2: contentsOfProperty2
}最后,清晰的沟通才是最重要的!所以,不管你想出什么名字(只要它们有合理的意义),我敢肯定,除非人们觉得有什么要证明的,否则他们不会对此感到厌烦。
发布于 2019-01-30 23:16:00
您应该能够使用Object.values over this.activeItem.json.schema.properties
Object.values()方法返回给定对象自身可枚举属性值的数组,其顺序与for...in循环提供的顺序相同(差异在于for-in循环也枚举原型链中的属性)。
const object1 = {
a: 'somestring',
b: 42,
c: false
};
console.log(Object.values(object1));
// expected output: Array ["somestring", 42, false]地图上还不支持它,但是如果需要的话,您应该能够加载聚脂填充。
https://stackoverflow.com/questions/54450383
复制相似问题