循环访问对象的键,然后遍历值(因为它们是数组)

时间:2018-05-24 19:27:31

标签: javascript

我有这个对象:



mDatabase.child("users").child(uid).child("user_photo").setValue(your_download_url);




所以,情况是这样的:示例有3个键。每个键都有一个对象数组。首先,我想循环遍历对象键(第一,第二,第三),然后是它们的数组(使用forEach)。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

Object.keys(object)将为对象提供数组的键。

public function getUsageData(Request $request)
{

    $start_date = $request->get('start_date');
    $end_date = $request->get('end_date');

    $particulars = DB::table('particulars')
        ->join('reqs', 'particulars.particular_id', "=", 'reqs.particular_id')
        ->whereBetween('date_applied', [$start_date, $end_date])
        ->select('particulars.item_name', 'particulars.unit', 'particulars.price', 'reqs.quantity_issued',
            DB::raw('particulars.price*reqs.quantity_issued AS total_cost'))
        ->get();
    if ($particulars->isEmpty()) {


        return "No Records Found...................... ";

    } else {

        $pdf = PDF::loadView('issuer.getUsageReport', ['particulars' => $particulars]);
        return $pdf->stream('getUsageReport.issuer');
    }
}

答案 1 :(得分:1)

要循环对象,您需要指定循环的内容,在这种情况下,我使用键

const example = {
  first: [{object: '1'},{object: '2'}],
  second: [{object: '3'},{object: '4'}]
}


Object.keys(example).forEach( entrie => {
  console.log(entrie)
  example[entrie].forEach(object => {
    console.log(object)
  })
})

希望这会有所帮助:>

BaseThrottle