0

I have two arrays of strings: hrefs, thumbs. Need to combine this arrays into another array with structure like below. How to do that ? For clarity - i need to use Lightview API to call function Lightview.show(elements), where elements is result array i need to build.

HTML:

<a href="/one" class="lv" thumbnail="one.jpg">one</a>
<a href="/two" class="lv" thumbnail="two.jpg">two</a>
<a href="/three" class="lv" thumbnail="three.jpg">three</a>

Arrays:

var hrefs = $('.lv').map(function() { return $(this).attr('href'); }).get();
var thumbs = $('.lv').map(function() { return $(this).attr('thumbnail'); }).get();

The desired result array (elements):

{
    {hrefs[0], 
        {
           thumbnail: thumbs[0]   
        }
    },
    {hrefs[1],
        {
           thumbnail: thumbs[1]   
        }
    },
    ...
}

I've started with this, but i think it is something wrong...

var e = new Array();
$.each(hrefs, function(i, value) {
    e[i] = new Array();
    e[i][0] = value;
    e[i][1] = {thumbnail: thumbs[i]};
});
6
  • 1
    Your desired result is not a valid Javascript/JSON structure. Can you clarify what you're looking for? Commented Jan 25, 2013 at 7:13
  • What is wrong with this structure ? Commented Jan 25, 2013 at 7:16
  • @marioosh can you give a concrete example of the desired output from the code you've shown so far? Commented Jan 25, 2013 at 7:18
  • I'm not as concerned about exactly valid syntax. I'm just not clear on what the desired data structure is. Commented Jan 25, 2013 at 7:18
  • 1
    What you're looking for can't exist in javascript. An object must be in the form {name:value}... you can't have {{name:value}}. You could do {name1:{name2:value}}. Commented Jan 25, 2013 at 7:24

4 Answers 4

2

Do you mean this?

var result = $('.lv').map(function() {
    var o = {};
    o[$(this).attr('href')] = { thumbnail: $(this).attr('thumbnail') } };
    return o;
}).get();
Sign up to request clarification or add additional context in comments.

2 Comments

Could be wrong but I'm pretty sure you have to use bracket notation for that code to work. var o = {}; o[$(this).attr('href')] = {thumb: ...}; return o; Or not?
I've done that before as well, it just seems like you should be able to... Usually you can reduce to one liner if readable enough like function func(o) { return o={}, o['foo'] = {a: 'baz'}, o; }
2

Are you looking for something like this?

var hrefs = $('.lv').map(function() { return $(this).attr('href'); }).get();
var thumbs = $('.lv').map(function() { return $(this).attr('thumbnail'); }).get();

var arr = {};
for (i in hrefs) {
    arr[hrefs[i]] = {
        thumbnail: thumbs[i]
    }
}

Another suggestion, with minimal use of JQuery for faster execution

var lvs = $('.lv');
var arr = {};
for (i=0; i< lvs.length; i++) {
    arr[lvs[i].getAttribute('href')] = {
        thumbnail: lvs[i].getAttribute('thumbnail')
    }
}

1 Comment

Good, +1 for vanilla javascript.
1

You're almost there. Just do:

var e = [];
$.each(hrefs, function(i, value) {
    e[i] = [];
    e[i] = [ value, {thumbnail: thumbs[i]} ];
});

Or even shorter:

var e = [];
$.each(hrefs, function(i, value) {
    e[i] = [ value, {thumbnail: thumbs[i]} ];
});

Comments

0

Try this

var result = {};

$.each( hrefs, function ( index ) {
    result[ hrefs[ index ] ] = { thumbnail: thumbs[ index ] };
});

Comments

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.