在下面的代码中,我可以获得对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 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
复制相似问题