Copy elements from one object to another and show it as json

Viewed 34

I have an object and I want to add new elements inside it and remove some elements

console.log("bookmark", bookmark)
[
   {
       "__type": "BookmarksApp.Bookmark",
       "BookmarkID": 1,
       "name": "San Diego, CA",
       "xmin": "-13048800",
       "ymin": "3844800",
       "xmax": "-13037800",
       "ymax": "3870400",
       "UserID": 6
   },
   {
       "__type": "BookmarksApp.Bookmark",
       "BookmarkID": 2,
       "name": "Redlands, California",
       "xmin": "-13052100",
       "ymin": "4024900",
       "xmax": "-13041100",
       "ymax": "4050500",
       "UserID": 6
   }
]

I trayed many ways but it doesn't work with me one of them like this:-

 bookmarkjson = {

                extent: {
                    xmin: JSON.stringify(bookmark.map(a => a.xmin)),
                    ymin: JSON.stringify(bookmark.map(a => a.ymin)),
                    xmax: JSON.stringify(bookmark.map(a => a.xmax)),
                    ymax: JSON.stringify(bookmark.map(a => a.ymax)),
                "spatialreference": {
                "wkid": 102100,
                "latestwkid": 3857
               },
             },
         name:bookmark.map(a => a.name),

 };

the result come like this all objects in one object how can I edit it :-

{
    "xmin": "[\"-13048800\",\"-13052100\"]",
    "ymin": "[\"3844800\",\"4024900\"]",
    "xmax": "[\"-13037800\",\"-13041100\"]",
    "ymax": "[\"3870400\",\"4050500\"]",
    "spatialreference": {
        "wkid": 102100,
        "latestwkid": 3857
    }
}

how can I change it to be like this

 {
                    first: {
                        "extent": {
                            "xmin": -12975100,
                            "ymin": 3993900,
                            "xmax": -12964100,
                            "ymax": 4019500,
                            "spatialReference": {
                                "wkid": 102100,
                                "latestWkid": 3857
                            }
                        },
                        "name": "Palm Springs, CA"
                    },
                    second: {
                        "extent": {
                            "xmin": -13052100,
                            "ymin": 4024900,
                            "xmax": -13041100,
                            "ymax": 4050500,
                            "spatialReference": {
                                "wkid": 102100,
                                "latestWkid": 3857
                            }
                        },
                        "name": "Redlands, California"
                    },
                   
                }

the bookmark is dynamic can has more than 2 objects

3 Answers

You can try something like this.

let bookmarks = [
   {
       "__type": "BookmarksApp.Bookmark",
       "BookmarkID": 1,
       "name": "San Diego, CA",
       "xmin": "-13048800",
       "ymin": "3844800",
       "xmax": "-13037800",
       "ymax": "3870400",
       "UserID": 6
   },
   {
       "__type": "BookmarksApp.Bookmark",
       "BookmarkID": 2,
       "name": "Redlands, California",
       "xmin": "-13052100",
       "ymin": "4024900",
       "xmax": "-13041100",
       "ymax": "4050500",
       "UserID": 6
   }
];

let result = bookmarks.map(x => { 
  let extend = 
  { 
    xmin: x.xmin, 
    ymin: x.ymin, 
    xmax: x.xmax, 
    ymax: x.ymax,
    spatialreference: {
      wkid: 102100,
      latestwkid: 3857
     }, 
     name: x.name 
  };
  return { extend: extend };
});

console.log(result)

Try this :

const arr = [
   {
       "__type": "BookmarksApp.Bookmark",
       "BookmarkID": 1,
       "name": "San Diego, CA",
       "xmin": "-13048800",
       "ymin": "3844800",
       "xmax": "-13037800",
       "ymax": "3870400",
       "UserID": 6
   },
   {
       "__type": "BookmarksApp.Bookmark",
       "BookmarkID": 2,
       "name": "Redlands, California",
       "xmin": "-13052100",
       "ymin": "4024900",
       "xmax": "-13041100",
       "ymax": "4050500",
       "UserID": 6
   }
];

const result = {};

arr.forEach((obj, index) => {
    result[index + 1] = {
    extent: {
        xmin: obj.xmin,
      ymin: obj.ymin,
      xmax: obj.xmax,
      ymax: obj.ymax,
      spatialReference: {
        wkid: 102100,
        latestWkid: 3857
      }
    },
    name: obj.name
  };
});

console.log(result);

you shoud not type cast it to string with stringify

you have an array of objects, and you want to create another one with the same length. so use map

let newBookmarkObj = bookmarkObj.map(item => {
  return {
          extend: {
            xmin: item.xmin,
            ymin: item.ymin,
            xmax: item.xmax,
            ymax: item.ymax,
            spatialReference: {
                wkid: 102100,
                latestWkid: 3857
            }
          },
          name: item.name
}
})
Related