简体   繁体   中英

How can I get the value of a sibling key in a javascript object?

I have the following object and want to grab the "payroll_run_items" where month equals "2022-04-01" so s.th. like the value of a sibling key where key values 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() {}"
}

All I came up with so far is to get all keys but how would I now check for "2022-04-01" and assign the "payroll_run_items" to a variable?

const payrollLineItems = () => (
        Object.keys(props.payrollRuns)
    )

find the object where the month matches the condition, and extract the property value you want.

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM