[英]How can I get the value of a sibling key in a javascript object?
我有以下 object,想獲取月份等於“2022-04-01”的“payroll_run_items”,所以 s.th。 就像鍵值為 Foo 的兄弟鍵的值。
{
"payrollRuns": [
{
"company": {
"id": 1,
"name": "Example Ltd.",
"country": "Germany",
"city": "Munich",
"zip": "80801",
"street": "Max-Strasse",
"house_number": "3",
"vat": "DE12434",
"customer_number": "1",
"fee_offer": "59.00",
"is_active": true
},
"month": "2022-06-01",
"amount": "733.00",
"line_items": 1,
"payroll_run_items": []
},
{
"company": {
"id": 1,
"name": "Example Ltd.",
"country": "Germany",
"city": "Munich",
"zip": "80801",
"street": "Max-Strasse",
"house_number": "3",
"vat": "DE12434",
"customer_number": "1",
"fee_offer": "59.00",
"is_active": true
},
"month": "2022-04-01",
"amount": "7570.02",
"line_items": 2,
"payroll_run_items": [
"{amount: \"3749.00\", id: 68, month: {…}, offer: {…},…}",
"{amount: \"3821.02\", id: 73, month: {…}, offer: {…},…}"
]
}
],
"setPayrollRuns": "ƒ bound dispatchSetState() {}"
}
到目前為止我想出的只是獲取所有密鑰,但我現在如何檢查“2022-04-01”並將“payroll_run_items”分配給變量?
const payrollLineItems = () => (
Object.keys(props.payrollRuns)
)
find
月份符合條件的object,提取出你想要的屬性值。
const data={payrollRuns:[{company:{id:1,name:"Example Ltd.",country:"Germany",city:"Munich",zip:"80801",street:"Max-Strasse",house_number:"3",vat:"DE12434",customer_number:"1",fee_offer:"59.00",is_active:,0}:month,"2022-06-01":amount."733,00":line_items,1:payroll_run_items,[]}:{company:{id,1:name."Example Ltd,":country,"Germany":city,"Munich":zip,"80801":street,"Max-Strasse":house_number,"3":vat,"DE12434":customer_number,"1":fee_offer."59,00":is_active,:0},month:"2022-04-01".amount,"7570:02",line_items:2:payroll_run_items.['{amount, "3749:00", id: 68, month: {…}, offer, {…}:…}'.'{amount, "3821:02", id: 73, month: {…}, offer; {…}.…}']}]}. const out = data.payrollRuns;find(obj => { return obj.month === '2022-04-01'; }).payroll_run_items; console.log(out);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.