0

hello to all i have this array

["..\Uploades\Product\1.jpg", "..\Uploades\Product\2.jpg", "..\Uploades\Product\30304_dz6AW8Tp.jpg"] 

i wanna convert it to this JSON

            [
    {
        "image": "..\Uploades\Product\1.jpg",
        "thumb": "..\Uploades\Product\1.jpg",
        "folder": "Small"
    },
  {
      "image": "..\Uploades\Product\2.jpg",
      "thumb": "..\Uploades\Product\2.jpg",
      "folder": "Small"
  },
    {
        "image": "..\Uploades\Product\30304_dz6AW8Tp.jpg",
        "thumb": "..\Uploades\Product\30304_dz6AW8Tp.jpg",
        "folder": "Small"
    }
            ]

what should i do?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
user3281649
  • 61
  • 1
  • 1
  • 3

2 Answers2

1

Try this

var arr= ["..\Uploades\Product\1.jpg", "..\Uploades\Product\2.jpg", "..\Uploades\Product\30304_dz6AW8Tp.jpg"];
var json = [];
$.each(arr, function(i,v){
    json.push({"image":v,"thumb":v,"folder":"small"});
});

console.log(json);

FIDDLE

Lokesh Suthar
  • 3,201
  • 1
  • 16
  • 28
1
var arr = ["..\\Uploades\\Product\\1.jpg", "..\\Uploades\\Product\\2.jpg", "..\\Uploades\\Product\\30304_dz6AW8Tp.jpg"] ;

You can use map to get an array of objects, and then stringify it to get JSON:

var JSON = JSON.stringify(arr.map(function(x) {
    return {image:x, thumb:x, folder:'Small'};
}));

Don't forget to shim Array.prototype.map and JSON.stringify if you need to support older browsers.

Paul
  • 139,544
  • 27
  • 275
  • 264