skip to Main Content

let images = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg"]

Convert this Encoded Array to this using JQuery…..
I have images encoded array, I want to add src: images like I mention down in the items collection.

          {
            src: '1.jpg'
          },
          {
            src: '2.jpg'
          },
          {
            src: '3.jpg'
          },
          {
            src: '4.jpg'
          },
          {
            src: '5.jpg'
          }
        ],```

2

Answers


  1. just try this

    var data= images.map(obj=> ({"src" : obj}));
    

    output

    [
      {
        "src": "1.jpg"
      },
      {
        "src": "2.jpg"
      },
      {
        "src": "3.jpg"
      },
      {
        "src": "4.jpg"
      },
      {
        "src": "5.jpg"
      }
    ]
    
    Login or Signup to reply.
  2. Here’s the jQuery equivalent

    let images = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"];
    let obj = {
      items: $.map(images, function(o) {
        return {
          src: o
        }
      })
    }
    console.log(obj)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search