gpt4 book ai didi

javascript - 将两个 javascript 合并为一个 foreach

转载 作者:行者123 更新时间:2023-11-28 04:37:02 24 4
gpt4 key购买 nike

我的 laravel Blade View 中有 javascript。我想知道是否可以将这两个脚本合并为一个。脚本在每个引导选项卡中显示图形。

首先编写脚本:

var chart1 = AmCharts.makeChart( "chart_visits", {
"dataProvider": [
@foreach($results as $result)
{
"date": "{{ date('Y-m-d', strtotime($result->time)) }}",
"value": '{{ $result->stats->visits }}'
},
@endforeach
]
} );

第二个脚本:

var chart1 = AmCharts.makeChart( "chart_visits", {
"dataProvider": [
@foreach($results as $result)
{
"date": "{{ date('Y-m-d', strtotime($result->time)) }}",
"value": '{{ $result->stats->bounceRate }}'
},
@endforeach
]
} );

这是我的观点:

<ul class="nav nav-tabs">
<li>
<a href="#bounce" data-toggle="tab"> Bounce rate </a>
</li>
<li class="active">
<a href="#visits" data-toggle="tab"> Visits </a>
</li>
</ul>

.

<div class="tab-content">
<div class="tab-pane active" id="visits">
<div class="chartdiv" id="chart_visits"></div>
</div>

<div class="tab-pane" id="bounce">
<div class="chartdiv" id="chart_bounce"></div>
</div>
</div>

最佳答案

如果我理解正确的话,最简单的方法就是从对象集合中创建普通数组并对其进行迭代。类似于:

var chart1 = AmCharts.makeChart( "chart_visits", {
"dataProvider": [
@foreach($results as $result)
{
"date": "{{ date('Y-m-d', strtotime($result['date'])) }}",
"value": "{{ $result['value'] }}"
},
@endforeach
]
} );

关于javascript - 将两个 javascript 合并为一个 foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44117082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com