从具有动态 ID 的嵌套对象中获取值 - javascript
Get values from nested object with dynamic ids - javascript
我正在从 SlackAPI(块元素)接收有效载荷,但我无法理解哦,我如何从中获取数据,因为 ID 和顺序总是在变化。
我想得到 protection_fee.value、legal_fee.value 和 repayment_date.selected_date
"state": {
"values": {
"CjV": {
"protection_fee": {
"type": "plain_text_input",
"value": "111"
}
},
"36tAM": {
"legal_fee": {
"type": "plain_text_input",
"value": "111"
}
},
"oH8": {
"repayment_date": {
"type": "datepicker",
"selected_date": "1990-04-18"
}
}
}
},
我尝试了 Object.keys 但显然我失败了,因为订单发生了变化。
当前代码:
const payload = JSON.parse(body);
const state = payload.state.values;
const first = Object.keys(state)[0];
const second = Object.keys(state)[1];
const repaymentDate = state[first].protection_fee.value;
const protectionFee = state[second].legal_fee.value;
我建议创建一个类似于 findProperty()
的函数,它将在有效负载中找到相关对象。
我们将在条目 key/value 对上调用 Object.entries()
on the payload.state.values
object and then using Array.find()
以查找具有所需 属性 的对象。
一旦我们有了属性,我们就可以return了。
let payload = { "state": { "values": { "CjV": { "protection_fee": { "type": "plain_text_input", "value": "111" } }, "36tAM": { "legal_fee": { "type": "plain_text_input", "value": "111" } }, "oH8": { "repayment_date": { "type": "datepicker", "selected_date": "1990-04-18" } } } } }
function findProperty(obj, key) {
const [, value] = Object.entries(obj).find(([k,v]) => v[key]);
return value[key];
}
console.log('legal_fee:', findProperty(payload.state.values, 'legal_fee').value)
console.log('protection_fee:', findProperty(payload.state.values, 'protection_fee').value)
.as-console-wrapper { max-height: 100% !important; }
我正在从 SlackAPI(块元素)接收有效载荷,但我无法理解哦,我如何从中获取数据,因为 ID 和顺序总是在变化。 我想得到 protection_fee.value、legal_fee.value 和 repayment_date.selected_date
"state": {
"values": {
"CjV": {
"protection_fee": {
"type": "plain_text_input",
"value": "111"
}
},
"36tAM": {
"legal_fee": {
"type": "plain_text_input",
"value": "111"
}
},
"oH8": {
"repayment_date": {
"type": "datepicker",
"selected_date": "1990-04-18"
}
}
}
},
我尝试了 Object.keys 但显然我失败了,因为订单发生了变化。
当前代码:
const payload = JSON.parse(body);
const state = payload.state.values;
const first = Object.keys(state)[0];
const second = Object.keys(state)[1];
const repaymentDate = state[first].protection_fee.value;
const protectionFee = state[second].legal_fee.value;
我建议创建一个类似于 findProperty()
的函数,它将在有效负载中找到相关对象。
我们将在条目 key/value 对上调用 Object.entries()
on the payload.state.values
object and then using Array.find()
以查找具有所需 属性 的对象。
一旦我们有了属性,我们就可以return了。
let payload = { "state": { "values": { "CjV": { "protection_fee": { "type": "plain_text_input", "value": "111" } }, "36tAM": { "legal_fee": { "type": "plain_text_input", "value": "111" } }, "oH8": { "repayment_date": { "type": "datepicker", "selected_date": "1990-04-18" } } } } }
function findProperty(obj, key) {
const [, value] = Object.entries(obj).find(([k,v]) => v[key]);
return value[key];
}
console.log('legal_fee:', findProperty(payload.state.values, 'legal_fee').value)
console.log('protection_fee:', findProperty(payload.state.values, 'protection_fee').value)
.as-console-wrapper { max-height: 100% !important; }