Tengo el siguiente objeto y quiero obtener "payroll_run_items" donde el mes es igual a "2022-04-01", así que s.th. como el valor de una clave hermana donde los valores clave 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() {}" }
Todo lo que se me ocurrió hasta ahora es obtener todas las claves, pero ¿cómo verificaría ahora "2022-04-01" y asignaría "payroll_run_items" a una variable?
const payrollLineItems = () => ( Object.keys(props.payrollRuns) )
find
el objeto donde el mes coincida con la condición y extraiga el valor de propiedad que desea.
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);