1

I need to create an array of arrays like this:

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.

3 Answers 3

2

Oh, what you did is... var test2 = $(this).contents(); is not the right thing you need to use. You should be using .data() and destructure the 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:

[
  [ 20, 10 ],
  [ 10, 20 ]
]
Sign up to request clarification or add additional context in comments.

Comments

0

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. 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. 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

Comments

0

Alternatively, you can use plain JS instead of 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.

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>

1 Comment

Code without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See How to Answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.