[英]How to rename Object Keys & change Object values from string to number
Alright, guys.好吧,伙计们。
I am dealing with <TimePicker/>
third party library from React
and want to manipulate the state so I can push this new format of code into a new object
later.我正在处理来自
React
<TimePicker/>
第三方库,并希望操纵状态,以便稍后我可以将这种新格式的代码推送到新object
。
when I target a value using this library, whatever time I pick, my state turns out to be a string like this - eg if I pick 20:30, this.state.time
equals "20:30"
.当我使用这个库定位一个值时,无论我选择什么时间,我的状态都是这样的字符串 - 例如,如果我选择 20:30,
this.state.time
等于"20:30"
。
I want to manipulate this string
into an object like this:我想将此
string
操作成这样的对象:
from "20:30"
to time: {hour: 20, minutes: 30}
///both hour and minutes are now numbers从
"20:30"
到time: {hour: 20, minutes: 30}
///小时和分钟现在都是数字
My full code below:我的完整代码如下:
import React from "react";
import TimePicker from "react-time-picker";
import './Clock.css'
class Clock extends React.Component {
state = {
time: '0:00',
pushTime: [],
value: ''
};
onChangeTimePickerHandler = time => {
this.setState({time});
let currentTime = this.state.time;
this.props.CallbackRenderTime(currentTime)
};
//I am using TimePicker library for React and want to manipulate the income from the chosen time
pushNewTimeHandler = () => {
let time = [...this.state.time] ;// ["1", "0", ":", "0", "0"]
time.splice(2, 1);
let timeToOneString = time.join().replace(/,/g, "");
let prepareStringForNewObject = (value, index) => {
let array = value.substring(0,index) + ',' + value.substring(index);
return array.split(',')
};
let newObj = {...prepareStringForNewObject(timeToOneString,2)};
console.log(newObj); // {0: "10", 1: "00"}
};
render() {
return (
<div>
<TimePicker
onChange={this.onChangeTimePickerHandler}
value={this.state.time}/>
<button onClick={this.pushNewTimeHandler}>Push the Array of Time</button>
<p>{this.state.time}</p>
</div>
)
}
}
export default Clock
So I have been playing around and came with this really ugly part-of-the-issue solution:所以我一直在玩,并提出了这个非常丑陋的问题部分解决方案:
//I am using TimePicker library for React and want to manipulate the income from the chosen time
manipulateTimeHandler = () => {
//this.state.time is "10:00"
let time = [...this.state.time] ;// now it looks like this: ["1", "0", ":", "0", "0"]
time.splice(2, 1);
let timeToOneString = time.join().replace(/,/g, "");
let prepareStringForNewObject = (value, index) => {
let array = value.substring(0,index) + ',' + value.substring(index);
return array.split(',')
};
let newObj = {...prepareStringForNewObject(timeToOneString,2)};
console.log(newObj); //finally it became the object, but the object values are still
//strings and the object keys still need to be changed into "hour"
//and "minutes" {0: "10", 1: "00"}
};
So on my ugly solution pushNewTimeHandler
I still need to rename the object keys to hour
and minutes
and also need to transform the string values for hours and minutes into numbers.所以在我丑陋的解决方案
pushNewTimeHandler
我仍然需要将对象键重命名为hour
和minutes
,还需要将小时和分钟的字符串值转换为数字。
I'd split the time string by :
, creating an array of strings, then map each string to a number, and destructure into hour
and minutes
on the left of the =
.我将通过
:
分割时间字符串,创建一个字符串数组,然后将每个字符串映射到一个数字,并在=
的左侧解构为hour
和minutes
。 Then you can make an object of hour
and minutes
:然后你可以制作一个
hour
和minutes
的对象:
const str = '20:30'; const [hour, minutes] = str.split(':').map(Number); const obj = { hour, minutes }; console.log(obj);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.