1

Hello all senior web developers, I am newbie for web development. I have used lightGallery plugin to view images. But my image path stored in array. In array element always dynamic that get from ajax response. This is My array :

     var data = { "result" : [ 
                                { "file_name"  : "a.jpg" },
                                { "file_name"  : "b.png"},
                                  ......................
                             ]
     }       

// This is Lightgallery array syntax

     $(this).lightGallery({
       dynamic: true,
       dynamicEl: [{"src": 'your image path'}, 
                   {'src': 'your image path'},
                    ........................
                  ]
     })

// I want like this :

     $(this).lightGallery({
       dynamic: true,
       dynamicEl: [{"src": 'a.jpg'}, 
                   {'src': 'b.jpg'},
                   ................
                  ]
     })

How to do that? Can or not?

thank for valuable help.

7
  • Have you tried to make objects from your ajax fetch and push into gallery array? Please add the code what you tried Commented Mar 23, 2018 at 1:38
  • Personally, if I control the data, I would make the response be the JSON you actually want, otherwise loop. Commented Mar 23, 2018 at 1:40
  • Yes, I have. but console show error wrong array format. Commented Mar 23, 2018 at 1:40
  • Here is my code : pastebin.com/ySeBVbjk Commented Mar 23, 2018 at 1:44
  • Is the file name corresponding to the full URL of your images? Commented Mar 23, 2018 at 1:49

2 Answers 2

1

Array.prototype.map() is convenient for creating new arrays based on originals.

See below for a practical example.

// Input.
const input = {
  result: [ 
    { "file_name"  : "a.jpg" },
    { "file_name"  : "b.png"}
  ]
}

// To Dynamic.
const toDynamic = ({result}) => ({
  dynamic: true,
  dynamicEl: result.map(x => ({src: x.file_name}))
})

// Output.
const output = toDynamic(input)

// Proof.
console.log(output)

Sign up to request clarification or add additional context in comments.

Comments

0

Your original var data is actually an object - see how it starts with a {.

const data = { "result" : [ 
  { "file_name"  : "a.jpg" },
  { "file_name"  : "b.png"},
]};
const dynamicElArr = data.result.map(({ file_name: src }) => ({ src }));
console.log(dynamicElArr);

Then you can use it for dynamicEl.

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.