[英]iterating problem in json data, iterating same data over and over
I have a iteration problem with json data. 我对json数据有一个迭代问题。 I send an array to javascript.
我将数组发送到javascript。 To use it convert it to json data and pass it to javascript var.
要使用它,请将其转换为json数据,然后将其传递给javascript var。
<script>
var estates = <?php echo json_encode($estates);?>;
function initMap(){
var options =
{
zoom : 6,
center : {lat:34.652500, lng:135.506302}
};
var map = new google.maps.Map(document.getElementById('map'), options);
@foreach ($estates as $est)
var marker = new google.maps.Marker({
map: map,
icon: 'imgs/marker.png',
url: "/pages/{{$est->id}}",
label: {
text: estates.data[0].price,
color: "#fff",
},
position: {
lat: {{$est->lat}},
lng: {{$est->lng}}
}
});
google.maps.event.addListener(marker, 'click', function () {
window.location.href = this.url;
});
@endforeach
}
</script>
But at this line estates.data[0].price,
my label is iterate same price over and over. 但是在此行
estates.data[0].price,
我的标签estates.data[0].price,
迭代相同的价格。 it's because i am giving [0] to it. 这是因为我给[0]。 But if I don't give a number then it doesn't go into
data
and couldn't find the price
. 但是如果我不给出数字,那它就不会进入
data
并找不到price
。 Is there anyway to iterate json. 无论如何有迭代json。 because now it giving me same price over and over like this
因为现在它给了我同样的价格遍地像这样
By the way I already tried the iteration below. 顺便说一下,我已经尝试了下面的迭代。 But this time every each marker label having the ALL prices at once...
但这一次每个标记标签一次具有所有价格...
for(let i = 0; i < estates.data.length; i++){
var marker = new google.maps.Marker({
map: map,
icon: 'imgs/marker.png',
url: "/pages/{{$est->id}}",
label: {
text: estates.data[i].price,
color: "#fff",
},
position: {
lat: {{$est->lat}},
lng: {{$est->lng}}
}
});
}
And also I tried this as well: 我也尝试过这个:
<?php
$js_array = json_encode($estates);
echo "var estates = ". $js_array . ";\n";
?>;
for(var i = 0; i < estates.data.length; i++){
var needle = estates.data[i];
for(var key in needle){
var obj = needle.price;
console.log(obj)
}
}
for(var itr = 0; itr < obj.length; itr++){
var marker = new google.maps.Marker({
map: map,
icon: 'imgs/marker.png',
url: "/pages/{{$est->id}}",
label: {
text: obj[itr],
color: "#fff",
},
position: {
lat: {{$est->lat}},
lng: {{$est->lng}}
}
});
}
But still not running properly.... 但是仍然无法正常运行。
Maybe you just have to wrap a for loop around it. 也许您只需要在其周围包装一个for循环即可。 I could not test it, because I don't have the PHP code.
我无法测试它,因为我没有PHP代码。
@foreach ($estates as $est)
for(let i = 0; i < estates.data.length; i++){
var marker = new google.maps.Marker({
map: map,
icon: 'imgs/marker.png',
url: "/pages/{{$est->id}}",
label: {
text: estates.data[i]
color: "#fff",
},
position: {
lat: {{$est->lat}},
lng: {{$est->lng}}
}
});
}
google.maps.event.addListener(marker, 'click', function () {
window.location.href = this.url;
});
Edit: Or you could try to edit this line text: {{$est->data}}
编辑:或者您可以尝试编辑以下行
text: {{$est->data}}
Edit² : Just delete the php stuff and only run in javascript? Edit² :仅删除php内容,仅在javascript中运行? I deleted the php foreach loop and just try to use the javascript object.
我删除了php foreach循环,只是尝试使用javascript对象。
<script>
var estates = <?php echo json_encode($estates);?>;
function initMap(){
var options =
{
zoom : 6,
center : {lat:34.652500, lng:135.506302}
};
var map = new google.maps.Map(document.getElementById('map'), options);
for(let i = 0; i < estates.length; i++){
var marker = new google.maps.Marker({
map: map,
icon: 'imgs/marker.png',
url: "/pages/"+estates[i].id,
label: {
text: estates.data[0].price,
color: "#fff",
},
position: {
lat: estates[i].lat,
lng: estates[i].lng
}
});
google.maps.event.addListener(marker, 'click', function () {
window.location.href = this.url;
});
}
}
</script>
I hope that helps 希望对您有所帮助
Solved the problem with a little touch. 轻触即可解决问题。
var marker = new google.maps.Marker({
map: map,
icon: 'imgs/marker.png',
url: "/pages/{{$est->id}}",
label: {
text: "{{$est->price}}",
color: "#fff",
},
position: {
lat: {{$est->lat}},
lng: {{$est->lng}}
}
});
Price is varchar, so used this loop in quotes "{{$est->price}}" and now need to json.encode also... Price是varchar,因此在“ {{$ est-> price}}”中使用了此循环,现在还需要json.encode ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.