dede.brahma
dede.brahma

Reputation: 329

How to set data object from response array data

i have response data like this

{
  "data": {
    "title": "dashboard",
    "description": "just test dashboard",
    "photos": [
      {
        "file_name": "12345454.jpg",
        "photo_name": "indonesia"
      },
      {
        "file_name": "567686.jpg",
        "photo_name": "jakarta"
      }]
  }
}

then i need change response of data with file_name of array without photos variable.

expected: file_name[0] will be sign from file_name index of array

like this

{
  "data": {
    "title": "dashboard",
    "description": "just test dashboard",
    "file_name[0]": "12345454.jpg",
    "file_name[1]": "567686.jpg"
  }
}

i have try with map of photos but didn't work

this.data = this.data.photos.map((item, idx) => {
    let data = {
      file_name: item.file_name
    }
    return data
  })
  this.data.photos.map((item, idx) => {
    this.$set(this.data, 'file_name', item.file_name)
  })

Upvotes: 0

Views: 1039

Answers (4)

Khurram Shaikh
Khurram Shaikh

Reputation: 300

have a look at this example. by using this you can restructure or create new you json.

<span id="text"></span><br/>
<span id="photosdata"></span>

here is the logic

var obj = JSON.parse('{   "data": {     "title": "dashboard",     "description": "just test dashboard",     "photos": [       {         "file_name": "12345454.jpg",         "photo_name": "indonesia"       },       {         "file_name": "567686.jpg",         "photo_name": "jakarta"       }]   } }');

document.getElementById("text").innerHTML = obj.data.title+"<br/>";

var photos = obj.data.photos;

photos.forEach(function(item){
 document.getElementById("text").innerHTML += item.file_name +"<br/>";
});

https://jsfiddle.net/qufnwmd4/1/

Upvotes: 0

Matt Way
Matt Way

Reputation: 33141

If I understand correctly, you actually want file_name to be an array. If so, and you are happy to alter the object in place, try:

const obj = {
  data: {
    title: 'dashboard',
    description: 'just test dashboard',
    photos: [
      {
        file_name: '12345454.jpg',
        photo_name: 'indonesia'
      },
      {
        file_name: '567686.jpg',
        photo_name: 'jakarta'
      }]
  },
}

obj.data.file_name = obj.data.photos.map(i => i.file_name)
delete obj.data.photos

console.log(obj)

Upvotes: 0

Orelsanpls
Orelsanpls

Reputation: 23515

I propose a soluce creating a new object.

@Nikhil Aggarwal soluce mutate the existing object

Both are good, pick your poison :)

const old = {
  data: {
    title: 'dashboard',
    description: 'just test dashboard',
    photos: [
      {
        file_name: '12345454.jpg',
        photo_name: 'indonesia',
      },
      {
        file_name: '567686.jpg',
        photo_name: 'jakarta',
      }],
  },
};

const formatted = {
  // We use reduce to create the data object
  data: Object.keys(old.data).reduce((tmp, x) => {
    // If we are not dealing with photos key, just add the old data to the new object
    if (x !== 'photos') {
      tmp[x] = old.data[x];
      
      return tmp;
    }
    
    // for each photo, add a new key
    old.data.photos.forEach((y, yi) => {
      tmp[`file_name[${yi}]`] = y.file_name;
    });
    
    return tmp;
  }, {}),
};

console.log(formatted);

Upvotes: 0

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28445

Try following

let data = {"title": "dashboard","description": "just test dashboard","photos":[{"file_name": "12345454.jpg","photo_name": "indonesia"},{"file_name": "567686.jpg","photo_name": "jakarta"}]};

data.photos.forEach((v,i) => data[`file_name[${i}]`] = v.file_name);
delete data.photos;
console.log(data);

Upvotes: 3

Related Questions