问题:
[ { "project_id": 1, "project_name": "CDP", "role": "PL" }, { "project_id": 2, "project_name": "Admincer", "role": "PM" },
我想使用某种方法将上述三个属性中的“project_id”属性添加到另一个数组中。
我的想法是: 1.首先,如果我可以将此数组的“project_id”属性复制到第二个嵌套 JSON 数组,那就可以了。
我查到的内容:
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);
我发现我可以这样复制它。 我在 Chrome 控制台中尝试了上面的代码。 数组被复制,但整个对象被复制。我只想复制project_id的属性。
我想在此数组中创建一个名为“prj_name”的新属性,并在 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粉9900084282024-04-07 14:53:33
您可以像 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})
这将创建 1 个合并的对象。
或者,如果您只想 project_id 值,则只需将其更改为:
objCopy.project_id = obj.project_id