[英]Is there a way to compare the corresponding integers in two numbers without converting them to arrays
I am trying to conditionally apply animations to integers in a number based on whether or not the value of the number goes up or down. 我试图有条件地将动画应用于数字中的整数,具体取决于数字的值是上升还是下降。
My first step has been to store my total in my component's state, then when I add one to it, the affected integer should slide upward and turn green. 我的第一步是将我的总数存储在组件的状态中,然后当我添加一个时,受影响的整数应向上滑动并变为绿色。 If I decrease by one, the affected integer should slide downward and turn red.
如果我减1,受影响的整数应向下滑动并变为红色。
I am trying to recreate the animation effect that Robinhood uses on its ticker when your portfolio value goes up or down, as it does in the first part of this gif . 我试图在你的投资组合价值上涨或下跌时重新创建Robinhood在其股票代码上使用的动画效果,就像在这个gif的第一部分一样。
I've gotten it to work but I am worried that my logic is not as efficient as it could be. 我已经开始工作,但我担心我的逻辑不尽如人意。 When I increase or decrease the total, before I set the new total I create a variable which represents the total plus or minus 1. I convert both the current total and the new total to strings so I can split them into arrays.
当我增加或减少总数时,在设置新总数之前,我创建一个表示总加减1的变量。我将当前总数和新总数转换为字符串,以便将其拆分为数组。 In another function I compare the two arrays by looping through the new total array and comparing the element at the current index in the loop to the element of the previous total array at the current index in the loop.
在另一个函数中,我通过循环遍历新的总数组并将循环中当前索引处的元素与循环中当前索引处的前一个总数组的元素进行比较来比较两个数组。
If the new total array's element at that index is larger, than I return true, if not I return false. 如果该索引处的新total数组元素较大,则返回true,否则返回false。 If it remains the same, I return null.
如果它保持不变,我返回null。 This array of trues, falses and nulls will correspond to the new total array in my render method.
这个trues,falses和null数组将对应于render方法中的新total数组。 At the same index, I pass down true, false or null to my child component and that determines the
className
the component will be assigned. 在同一个索引处,我将true,false或null传递给我的子组件,并确定将分配组件的
className
。
Here is my parent component: 这是我的父组件:
class Parent extends React.Component{
me.state = {
isCreateScenarioModalVisible: false,
isExpressModalVisible: false,
total: 0,
truesArr: []
};
checkNums = (a, b) =>{
var bNum = parseInt(b.reverse().join(""))
var aNum = parseInt(a.reverse().join(""))
var arr = a.map((el, i)=>{
if (a.length === b.length){
if (aNum < bNum && parseInt(a[i]) < parseInt(b[i])) {
return false
} else if ( aNum > bNum && parseInt(a[i]) > parseInt(b[i])) {
return true
} else if ((aNum < bNum && parseInt(a[i]) === parseInt(b[i]))
|| (aNum < bNum && parseInt(a[i]) === parseInt(b[i]))) {
return null
} else {
return null
}
} else if (a.length > b.length){
return true
} else {
return false
}
})
this.setState({
truesArr: arr
})
}
incrementTotal = (arg) =>{
let num = this.state.total + arg
this.checkNums(num.toString().split("").reverse(),
this.state.total.toString().split("").reverse())
this.setState({
total: num
}, ()=>{
console.log(this.state.truesArr)
})
setTimeout(()=>{
this.setState({
truesArr: []
})
}, 500)
}
render (){
return(
<div>
<div>
{this.state.total.toString().split("").map((num, i)=>{
return <Num key={i} num={num} color=
{this.state.truesArr[i]}/>
})}
</div>
<div onClick={()=>this.incrementTotal(1)}>+1</div>
<div onClick={()=>this.incrementTotal(-1)}>-1</div>
</div>
)
}
}
export default Parent;
And here is my child component: 这是我的孩子组成部分:
class Num extends React.Component {
constructor(prop) {
super(prop);
}
render() {
const self = this;
let className
if (this.props.color === true){
className = 'green'
} else if (this.props.color === false) {
className = 'red'
} else {
className = 'norm'
}
return (
<p className={className}>{this.props.num}</p>
);
}
}
export default Num;
I have omitted the CSS code because it doesn't affect the logic in my components. 我省略了CSS代码,因为它不会影响我的组件中的逻辑。
As of now it's working correctly (although there are some bugs when you get into negative numbers that I am working on fixing) but I wanted to know if there was a more efficient way to compare the integers within two numbers. 截至目前它正常工作(虽然当你进入我正在修复的负数时会有一些错误)但我想知道是否有更有效的方法来比较两个数字内的整数。 For example, if I am given 1000 and 1001, I want to be able to compare each of the integers at their respective positions to see if they're bigger than the corresponding integer of the other number.
例如,如果给出1000和1001,我希望能够比较各自位置的每个整数,看它们是否大于另一个数字的相应整数。 Like so:
像这样:
1000 => 1 0 0 0 1000 => 1 0 0 0
1001 => 1 0 0 1 1001 => 1 0 0 1
compare=> == == == 0 < 1 compare => == == == 0 <1
value => null null null false value => null null null false
In this scenario, only the last number will be given an animation (and in this case, the animation will be downward turning and red) 在这种情况下,只有最后一个数字将被赋予动画(在这种情况下,动画将向下转动并且红色)
You could take the digits and compare them 你可以拿数字并比较它们
function compare(a, b) { const digit = i => v => Math.floor(v / Math.pow(10, i)) % 10; return Array .from( { length: Math.ceil(Math.log10(Math.max(a, b))) }, (_, i) => ((l, r) => l === r ? null : l > r) (...[a, b].map(digit(i))) ) .reverse(); } console.log(compare(1001, 1000)); // null null null true console.log(compare(1000, 1001)); // null null null false console.log(compare(1000, 2005)); // false null null false console.log(compare(0, 123450)); // false false false false false
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could convert your numbers by splitting them as string and put each digit back into numbers : 您可以通过将它们拆分为字符串来转换您的数字,并将每个数字放回数字:
function checkNums(a, b) { b = b.toString().split('').map(Number) return a.toString().split('').map(Number).map((digit, index) => { if (digit < b[index]) return true if (digit > b[index]) return false return null }) } console.log(checkNums(1001, 1000)) console.log(checkNums(1111, 1000)) console.log(checkNums(1001, 1010)) console.log(checkNums(1000, 1000)) console.log(checkNums(1000, 9999))
However, this solution will not work if you have a different amount of digits in both arrays. 但是,如果两个阵列中的数字位数不同,则此解决方案将不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.