摆脱对象值我有一个名为data
这样的对象:JS - 如何通过关键
var data = {
operators: {
operator1: {
top: 20,
left: 20,
properties: {
title: 'Operator 1',
inputs: {},
outputs: {
output_1: {
label: 'Output 1',
},
output_2: {
label: 'Output 2',
}
}
}
},
operator2: {
top: 80,
left: 300,
properties: {
title: 'Operator 2',
inputs: {
input_1: {
label: 'Input 1',
},
input_2: {
label: 'Input 2',
},
input_3: {
label: 'Input 3',
},
},
outputs: {}
}
},
},
};
如何通过一键得到data
的值。
function myFunction(data, key) {
//Find in data with key, and return value.
}
结果:var result = myFunction(data,'output_1')
= Output 1
(按标题获取)。
_ “价值像JSON数据” _ - 这是一个_object_。 ;) – evolutionxbox
检查这个问题的答案:[按属性名称递归搜索对象中的值](https://stackoverflow.com/questions/40603913/search-recursively-for-value-in-object-by-property-名称) –
当您将密钥传递给函数时,“通过标题获取”是什么意思。 – muaaz