简体   繁体   English

如何用数据形成 arrays 的数组

[英]how to form an array of arrays with data

I need to create an array of arrays like this:我需要像这样创建一个 arrays 数组:

var serviceCoors = [
        [50, 40],
        [50, 50],
        [50, 60],
    ];

from elements with datas:从带有数据的元素:

<div data-latitude="10" data-longitude="20" class="service-points__map"></div>
<div data-latitude="20" data-longitude="10" class="service-points__map"></div>

I`m trying this:我正在尝试这个:

var test = [];
$('.service-points__map').each(function(){
        var test2 = $(this).contents();
        var items = [];
        $.each(test2, function(i, val) {
            items.push(val.data);
        });
        test.push(items);
    });

But it doesn`t work.但它不起作用。 I have 2 arrays, but they are empty.我有 2 个 arrays,但它们是空的。

Oh, what you did is... var test2 = $(this).contents();哦,你所做的是... var test2 = $(this).contents(); is not the right thing you need to use.不是你需要使用的正确的东西。 You should be using .data() and destructure the object :您应该使用.data()解构 object

 var test = []; $('.service-points__map').each(function() { var { longitude, latitude } = $(this).data(); test.push([longitude, latitude]); }); console.log(test);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div data-latitude="10" data-longitude="20" class="service-points__map"></div> <div data-latitude="20" data-longitude="10" class="service-points__map"></div>

I got the output as:我得到了 output 为:

[
  [ 20, 10 ],
  [ 10, 20 ]
]

Initially I was going to suggest jQuery's map method but - by design apparently - it automatically flattens the output which can only be solved by nesting your coordinates in another array.最初我打算建议jQuery 的map方法,但是 -显然是设计- 它会自动展平 output 只能通过将坐标嵌套在另一个数组中来解决。 It also seems like it automatically coerces strings to numbers which is useful in your case but it seems to be making a lot of decisions that you might not want.它似乎也自动将字符串强制转换为数字,这在您的情况下很有用,但它似乎做出了很多您可能不想要的决定。

 const out = $('div').map((i, el) => { const { latitude, longitude } = $(el).data(); return [[ latitude, longitude ]]; }).toArray(); console.log(out);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div data-latitude="10" data-longitude="20" class="service-points__map"></div> <div data-latitude="20" data-longitude="10" class="service-points__map"></div>

Native JS doesn't have this problem. Native JS 没有这个问题。 Just pick up the elements, and iterate over them returning the new array of nested coordinates.只需拿起元素,然后遍历它们,返回嵌套坐标的新数组。

 const divs = document.querySelectorAll('div'); const coords = Array.from(divs).map(div => { const { latitude, longitude } = div.dataset; return [ +latitude, +longitude ]; }); console.log(coords);
 <div data-latitude="10" data-longitude="20" class="service-points__map"></div> <div data-latitude="20" data-longitude="10" class="service-points__map"></div>

Additional documentation附加文件

Alternatively, you can use plain JS instead of jQuery.或者,您可以使用纯 JS 代替 jQuery。 All you need to do is to get elements, iterate over them, create an array of the attributes of each item, and push it into the test array.您需要做的就是获取元素,对其进行迭代,创建每个项目的属性数组,并将其推送到test数组中。

 const test = []; const items = document.querySelectorAll('.service-points__map'); items.forEach(item => { test.push([ parseInt(item.getAttribute('data-latitude')), parseInt(item.getAttribute('data-longitude')), ]); }); console.log(test);
 <div data-latitude="10" data-longitude="20" class="service-points__map"></div> <div data-latitude="20" data-longitude="10" class="service-points__map"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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