How to change JSON nested array properties "project_id" and "project_name"
P粉794851975
P粉794851975 2024-04-06 15:39:51
0
1
473

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粉794851975
P粉794851975

reply all(1)
P粉990008428

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!