vue.js - 如何通过lodash映射并在prop值中返回false
问题描述
在我的回应中:
{
"1": [
{
"id": 11,
"seniorcitizen_id": 67201379,
"courier_id": 33,
"totalPrice": 500,
"delivery_date": "June 14, 2018, 12:00 am",
"packed": 0,
"parcel_id": 1,
"medicine_id": 1,
"qty": 3,
"owner": {
"id": 67201379,
"barangay_id": 1,
"first_name": "Cloyd",
"middle_name": "O'Keefe",
"last_name": "Hackett",
"gender": "female",
"mobile": "639153353163",
"telephone": "877-710-3958",
"caretakers_name": "Prof. Laurence Kertzmann",
"caretakers_mobile_phone": "639153353163",
"lat": "10.30431500",
"lng": "123.89035500",
"address": "213 Marlon Forks\nSouth Corineland, HI 81723-1044",
"budget": 1000,
"status": 1,
"dob": "-0001-11-30 00:00:00",
"created_at": "2017-10-19 18:09:10",
"updated_at": "2017-10-19 18:09:10"
}
},
{
"id": 12,
"seniorcitizen_id": 67201379,
"courier_id": 33,
"totalPrice": 500,
"delivery_date": "June 14, 2018, 12:00 am",
"packed": 0,
"parcel_id": 1,
"medicine_id": 5,
"qty": 2,
"owner": {
"id": 67201379,
"barangay_id": 1,
"first_name": "Cloyd",
"middle_name": "O'Keefe",
"last_name": "Hackett",
"gender": "female",
"mobile": "639153353163",
"telephone": "877-710-3958",
"caretakers_name": "Prof. Laurence Kertzmann",
"caretakers_mobile_phone": "639153353163",
"lat": "10.30431500",
"lng": "123.89035500",
"address": "213 Marlon Forks\nSouth Corineland, HI 81723-1044",
"budget": 1000,
"status": 1,
"dob": "-0001-11-30 00:00:00",
"created_at": "2017-10-19 18:09:10",
"updated_at": "2017-10-19 18:09:10"
}
},
{
"id": 13,
"seniorcitizen_id": 67201379,
"courier_id": 33,
"totalPrice": 500,
"delivery_date": "June 14, 2018, 12:00 am",
"packed": 0,
"parcel_id": 1,
"medicine_id": 6,
"qty": 3,
"owner": {
"id": 67201379,
"barangay_id": 1,
"first_name": "Cloyd",
"middle_name": "O'Keefe",
"last_name": "Hackett",
"gender": "female",
"mobile": "639153353163",
"telephone": "877-710-3958",
"caretakers_name": "Prof. Laurence Kertzmann",
"caretakers_mobile_phone": "639153353163",
"lat": "10.30431500",
"lng": "123.89035500",
"address": "213 Marlon Forks\nSouth Corineland, HI 81723-1044",
"budget": 1000,
"status": 1,
"dob": "-0001-11-30 00:00:00",
"created_at": "2017-10-19 18:09:10",
"updated_at": "2017-10-19 18:09:10"
}
}
],
"11": [
{
"id": 18,
"seniorcitizen_id": 67201376,
"courier_id": 33,
"totalPrice": 400,
"delivery_date": "June 14, 2018, 12:00 am",
"packed": 0,
"parcel_id": 11,
"medicine_id": 4,
"qty": 2,
"owner": {
"id": 67201376,
"barangay_id": 1,
"first_name": "Jess",
"middle_name": "Kris",
"last_name": "Berge",
"gender": "female",
"mobile": "93332998610",
"telephone": "(855) 951-3687",
"caretakers_name": "Brad O'Connell",
"caretakers_mobile_phone": "9332998610",
"lat": "10.30309100",
"lng": "123.89154500",
"address": "1291 Stephania Road\nLake Dorotheastad, TN 82682-76",
"budget": 1000,
"status": 1,
"dob": "-0001-11-30 00:00:00",
"created_at": "2017-10-19 18:09:09",
"updated_at": "2017-10-19 18:09:09"
}
},
{
"id": 19,
"seniorcitizen_id": 67201376,
"courier_id": 33,
"totalPrice": 400,
"delivery_date": "June 14, 2018, 12:00 am",
"packed": 0,
"parcel_id": 11,
"medicine_id": 5,
"qty": 2,
"owner": {
"id": 67201376,
"barangay_id": 1,
"first_name": "Jess",
"middle_name": "Kris",
"last_name": "Berge",
"gender": "female",
"mobile": "93332998610",
"telephone": "(855) 951-3687",
"caretakers_name": "Brad O'Connell",
"caretakers_mobile_phone": "9332998610",
"lat": "10.30309100",
"lng": "123.89154500",
"address": "1291 Stephania Road\nLake Dorotheastad, TN 82682-76",
"budget": 1000,
"status": 1,
"dob": "-0001-11-30 00:00:00",
"created_at": "2017-10-19 18:09:09",
"updated_at": "2017-10-19 18:09:09"
}
}
],
"13": [
{
"id": 21,
"seniorcitizen_id": 67201380,
"courier_id": 33,
"totalPrice": 350,
"delivery_date": "June 14, 2018, 12:00 am",
"packed": 0,
"parcel_id": 13,
"medicine_id": 2,
"qty": 2,
"owner": {
"id": 67201380,
"barangay_id": 1,
"first_name": "Ryann",
"middle_name": "Kub",
"last_name": "Bauch",
"gender": "male",
"mobile": "9332998610",
"telephone": "1-855-475-0137",
"caretakers_name": "Mr. Jarrett Ziemann II",
"caretakers_mobile_phone": "9332998610",
"lat": "10.30356400",
"lng": "123.89964100",
"address": "20330 Schmeler Course Apt. 210\nNorth Ari, NV 70048",
"budget": 1000,
"status": 1,
"dob": "-0001-11-30 00:00:00",
"created_at": "2017-10-19 18:09:10",
"updated_at": "2017-10-19 18:09:10"
}
},
{
"id": 22,
"seniorcitizen_id": 67201380,
"courier_id": 33,
"totalPrice": 350,
"delivery_date": "June 14, 2018, 12:00 am",
"packed": 0,
"parcel_id": 13,
"medicine_id": 19,
"qty": 2,
"owner": {
"id": 67201380,
"barangay_id": 1,
"first_name": "Ryann",
"middle_name": "Kub",
"last_name": "Bauch",
"gender": "male",
"mobile": "9332998610",
"telephone": "1-855-475-0137",
"caretakers_name": "Mr. Jarrett Ziemann II",
"caretakers_mobile_phone": "9332998610",
"lat": "10.30356400",
"lng": "123.89964100",
"address": "20330 Schmeler Course Apt. 210\nNorth Ari, NV 70048",
"budget": 1000,
"status": 1,
"dob": "-0001-11-30 00:00:00",
"created_at": "2017-10-19 18:09:10",
"updated_at": "2017-10-19 18:09:10"
}
}
]
}
使用此数据:我想使用 lodash 进行映射,如果打包为 0,则返回 false,如果为 1,则返回 true。
canToggle(){
_.map(this.parcels, function(status) {
var parcelState ;
status.packed === 0 ? parcelState = true : parcelState = false ;
return parcelState ;
});
},
并将其传递给我的组件道具,例如:
<delivery-parcel :parcel="parcel" :index="index" :packed="canToggle"></delivery-parcel>
所以在我通过的道具中:它会像:
props: ['packed','index','parcel'],
data(){
return {
isPacked : this.packed
}
并在我的复选框中使用它,以便查看包裹是否已打包,以判断该特定包裹是否已打包。
<v-checkbox @click="togglePacked" v-model="isPacked"></v-checkbox>
解决方案
我将假设 packed 将是 0 或 1 而不是任何其他数字。Vuejs 在控制台中分配 true 和 false 时存在问题,所以我建议这样做。创建一个计算属性,它将检查打包的变量值。
computed: {
packStatus: function () {
return this.packed !== 0 //this should return true if it's not 0, else return false
}
}
然后您可以通过
<delivery-parcel :parcel="parcel" :index="index" :packed=packStatus></delivery-parcel>
推荐阅读
- javascript - useState 中的默认值未重新初始化
- swift - 动画消失时不起作用,但出现时起作用
- javascript - Fullcalendar v5 如何使用复选框显示和隐藏事件
- asp.net - 如何将图像上传到 Razor 页面中的模型?
- javascript - 将数据放在文本框上编辑/更新模态 Laravel
- c - CS50 pset4 滤镜(不太舒服) - 模糊功能不起作用
- javascript - 比较两个数组的元素,结果返回不正确的值,为什么?
- django - 使用 Daphne 部署 Django+Channels+Heroku:关闭 Postgre 连接
- azure - Azure AKS:分配静态 IP 后,Kubernetes pod 一直显示 CrashLoopBackOff 状态
- go - 如何在 ctx 处理 websocket 超时