A
A
alex5e2015-10-14 22:04:28
JavaScript
alex5e, 2015-10-14 22:04:28

How to generate json array by selector?

Please tell me how to generate an array of several json objects, which is the src attribute for images with a certain class, i.e. all to be generated into an array of type$('.detail-image-fancy').attr('src')

[
{href : 'src'},
{href : 'src'},
{href : 'src'},
]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Ineshin, 2015-10-14
@alex5e

var urls = [];

$('.detail-image-fancy').each(function () {
    var src = $(this).prop('src');
    urls.push({href: src});
});

var json = JSON.stringify(urls);

V
Vitaly Inchin ☢, 2015-10-15
@In4in

//Псевдомассив jQuery 
var data = $.map($('.detail-image-fancy'), function(e){
  return {href : e.src};
});

//Нормальный массив
var data = [].map.call($('.detail-image-fancy'), function(e){
  return {href : e.src};
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question