我有以下对象类型:
{
2019-02-28 02:36:20: "5 minutes"
2019-02-28 23:59:59: "Today"
2019-03-01 02:31:20: "+1 Day"
2019-03-02 02:31:20: "+2 Days"
2019-03-03 02:31:20: "+3 Days"
2019-03-07 02:31:20: "+1 Week"
2019-03-14 02:31:20: "+2 Weeks"
2019-03-21 02:31:20: "+3 Weeks"
2019-03-28 02:31:20: "+1 Month"
2019-04-28 02:31:20: "+2 Months"
2019-05-28 02:31:20: ">2 Months"
}
我想将其转换为:
[
{
label:'5 minutes',
value:'2019-02-28 02:36:20'
},
{
label:'Today',
value:'2019-02-28 23:59:59'
},
]
我只能通过下面使用此功能获得键和值,但我无法创建该类型的数组。请任何人都可以帮我。
closeDate = Object.values(state.finalRequest.closeDate);
closeDateKey = Object.keys(state.finalRequest.closeDate);
您可以使用 object.entries ,然后使用 MAP
以所需的形式映射
let obj = {"2019-02-28 02:36:20": "5 minutes","2019-02-28 23:59:59": "Today","2019-03-01 02:31:20": "+1 Day","2019-03-02 02:31:20": "+2 Days","2019-03-03 02:31:20": "+3 Days","2019-03-07 02:31:20": "+1 Week","2019-03-14 02:31:20": "+2 Weeks","2019-03-21 02:31:20": "+3 Weeks","2019-03-28 02:31:20": "+1 Month","2019-04-28 02:31:20": "+2 Months","2019-05-28 02:31:20": ">2 Months"}
let op = Object.entries(obj)
.map(([ label, value ] ) => ({ label, value }))
console.log(op)
在这里,您使用 for ... in 迭代输入对象:
const input = {
"2019-02-28 02:36:20": "5 minutes",
"2019-02-28 23:59:59": "Today",
"2019-03-01 02:31:20": "+1 Day",
"2019-03-02 02:31:20": "+2 Days",
"2019-03-03 02:31:20": "+3 Days",
"2019-03-07 02:31:20": "+1 Week",
"2019-03-14 02:31:20": "+2 Weeks",
"2019-03-21 02:31:20": "+3 Weeks",
"2019-03-28 02:31:20": "+1 Month",
"2019-04-28 02:31:20": "+2 Months",
"2019-05-28 02:31:20": ">2 Months"
};
let res = [];
for (const key in input)
{
res.push({label:input[key], value:key});
}
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}