Javascript 我如何优化“慢”呢;“用于循环”;当比较两个对象时?
我试着比较两个对象,看看是否有相同的值。 我的对象是这样的:Javascript 我如何优化“慢”呢;“用于循环”;当比较两个对象时?,javascript,arrays,reactjs,react-native,object,Javascript,Arrays,Reactjs,React Native,Object,我试着比较两个对象,看看是否有相同的值。 我的对象是这样的: 例如,我需要获取object\u a[2]和object\u a[4]并将它们保存在状态变量中,我正在使用react native 这是到目前为止我的代码,它按预期工作,但速度很慢,当我的组件装入时,至少需要1s才能运行此代码 state = { theScore: null, theDescription: '', checked: null, }; async getFromStorage() {
例如,我需要获取
object\u a[2]
和object\u a[4]
并将它们保存在状态变量中,我正在使用react native
这是到目前为止我的代码,它按预期工作,但速度很慢,当我的组件装入时,至少需要1s
才能运行此代码
state = {
theScore: null,
theDescription: '',
checked: null,
};
async getFromStorage() {
try {
let jsonVal = await AsyncStorage.getItem('@answer');
if (jsonVal !== null) {
return JSON.parse(jsonVal);
}
} catch (e) {
}
};
componentDidMount() {
InteractionManager.runAfterInteractions(() => {
const myRoute = this.props;
this.getFromStorage().then(((storeValue) => {
if (storeValue !== null) {
if (storeValue.length > 0) {
for (let i = 0; i < myRoute.choices.length; i++) {
if (storeValue.some(storage => storage.choice_id === myRoute.choices[i].id)) {
this.setState({checked: myRoute.choices[i].id},
function () {});
this.setState({theScore: myRoute.choices[i].score},
function () {});
}
}
for (let i = 0; i < storeValue.length; i++) {
if (myRoute.choices.some(choice => choice.question_id ===
storeValue[i].question_id) &&
storeValue[i].description !== '') {
this.setState({theDescription: storeValue[i].description},
function () {});
}
}
}
}
}));
});
}
状态={
核心:空,
描述:'',
选中:空,
};
异步getFromStorage(){
试一试{
让jsonVal=wait AsyncStorage.getItem(“@answer”);
if(jsonVal!==null){
返回JSON.parse(jsonVal);
}
}捕获(e){
}
};
componentDidMount(){
InteractionManager.runAfterInteractions(()=>{
const myRoute=this.props;
this.getFromStorage()。然后(((storeValue)=>{
if(storeValue!==null){
如果(storeValue.length>0){
for(设i=0;istorage.choice_id===myRoute.choices[i].id)){
this.setState({checked:myRoute.choices[i].id},
函数(){});
this.setState({theScore:myRoute.choices[i].score},
函数(){});
}
}
for(设i=0;ichoice.question\u id===
storeValue[i]。问题(id)&&
storeValue[i]。说明!=''){
this.setState({theDescription:storeValue[i].description},
函数(){});
}
}
}
}
}));
});
}
如您所见,我对循环使用了两个
,因为计数需要不同,而且我还使用some()
检查object\u b
中的值是否存在于object\u a
中。我不知道是否有更好的方法来做这件事。谢谢你的时间 您可以像下面的代码那样修改代码
if (storeValue !== null) {
if (storeValue.length > 0) {
let checked, theScore, theDescription;
for (let i = 0; i < myRoute.choices.length; i++) {
if (storeValue.some(storage => storage.choice_id === myRoute.choices[i].id)) {
checked = myRoute.choices[i].id;
theScore = myRoute.choices[i].score;
}
}
for (let i = 0; i < storeValue.length; i++) {
if (
myRoute.choices.some(choice => choice.question_id === storeValue[i].question_id) &&
storeValue[i].description !== ''
) {
theDescription = storeValue[i].description;
}
}
this.setState({ checked, theScore, theDescription });
}
}
if(storeValue!==null){
如果(storeValue.length>0){
让我们检查一下,核心,描述;
for(设i=0;istorage.choice_id===myRoute.choices[i].id)){
checked=myRoute.choices[i].id;
theScore=myRoute.choices[i]。分数;
}
}
for(设i=0;ichoice.question\u id===storeValue[i]。question\u id)&&
storeValue[i]。说明!=''
) {
描述=存储值[i]。描述;
}
}
this.setState({checked,theScore,theDescription});
}
}
您不应该在同一个函数中多次调用this.setState
试着总结一下,在函数结束时只调用一次
谢谢你的精彩提示。它帮助很大,但仍然很慢,我可以用更好的方法来循环吗?可能是某个方法而不是some()
?它仍然会变慢,因为异步存储。getItem
是一个异步函数,它需要时间来完成哦,我明白了。。。谢谢
if (storeValue !== null) {
if (storeValue.length > 0) {
let checked, theScore, theDescription;
for (let i = 0; i < myRoute.choices.length; i++) {
if (storeValue.some(storage => storage.choice_id === myRoute.choices[i].id)) {
checked = myRoute.choices[i].id;
theScore = myRoute.choices[i].score;
}
}
for (let i = 0; i < storeValue.length; i++) {
if (
myRoute.choices.some(choice => choice.question_id === storeValue[i].question_id) &&
storeValue[i].description !== ''
) {
theDescription = storeValue[i].description;
}
}
this.setState({ checked, theScore, theDescription });
}
}