Home  >  Q&A  >  body text

How to change JSON nested array properties "project_id" and "project_name"

question:

[
{
"project_id": 1,
"project_name": "CDP",
"role": "PL"
},
{
"project_id": 2,
"project_name": "Admincer",
"role": "PM"
},

I want to use some method to add the "project_id" attribute among the above three attributes to another array.

My thoughts are: 1. First, if I could copy the "project_id" attribute of this array to the second nested JSON array, that would be fine.

What I found:

const obj = {
"project_id": 1,
"project_name": "CDP",
"role": "PL"
};;
const objCopy = {
"start_time": "09:00:00",
"end_time": "18:00:00",
"rest_time": "01:00:00",
"worked_time": "08:00:00",
"is_wfh": true,
"id": 1, 1,
"work_day_id": 45,
"time_cards": [
{
... obj
}
]
};;
console.log (objCopy);

I found I could replicate it like this. I tried the above code in Chrome console. The array is copied, but the entire object is copied. I just want to copy the attribute of project_id.

I want to create a new property called "prj_name" in this array and display only that property in Vuetify.

async fetchWorkerTimeCard() {
  try {
    this.worker_data = [] 
    await this.$axios.$get('/worker_time_card', {
      params: {
        work_date: this.calendarVal
      }
    }).then(data => {
      this.worker_data = data
    })
    var projects = await this.fetch_worker_projects()
    console.log(projects)
  } catch (error) {
    console.log(error)
    this.worker_data = []
  }
},
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.5/vue.js"></script>
<v-card>
  <v-data-table v-if="worker_data.time_cards" :headers="headers2" :items="worker_data.time_cards"></v-data-table>
</v-card>

P粉794851975P粉794851975108 days ago320

reply all(1)I'll reply

  • P粉990008428

    P粉9900084282024-04-07 14:53:33

    You can simply change object data like any other object in JS.

    const obj = {
      "project_id": 1,
      "project_name": "CDP",
      "role": "PL"
    };
    
    const objCopy = {
      "start_time": "09:00:00",
      "end_time": "18:00:00",
      "rest_time": "01:00:00",
      "worked_time": "08:00:00",
      "is_wfh": true,
      "id": 1,
      "work_day_id": 45
    }
    
    console.log({...obj, ...objCopy})

    This will create 1 merged object.

    Or if you just want the project_id value, just change it to:

    objCopy.project_id = obj.project_id

    reply
    0
  • Cancelreply