Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中的操作问题_Javascript_React Native - Fatal编程技术网

Javascript中的操作问题

Javascript中的操作问题,javascript,react-native,Javascript,React Native,最近,我正在使用javascript。 这是我的例子 工作代码: const start = index - 4 >= 0 ? index - 4 : 0 const end = index + 4 > this.props.initialMarkers.length ? this.props.initialMarkers.length : index + 4 for (let i = start; i <=

最近,我正在使用javascript。 这是我的例子

工作代码:

const start = index - 4 >= 0 ? index - 4 : 0
const end =   index + 4 > this.props.initialMarkers.length
            ? this.props.initialMarkers.length
            : index + 4
        for (let i = start; i <= end; i++) {
          newData.push(this.props.initialMarkers[i])
        }
    for (let i = index - 4 >= 0 ? index - 4 : 0; i <= index + 4 > this.props.initialMarkers.length
        ? this.props.initialMarkers.length
        : index + 4; i++) {
      newData.push(this.props.initialMarkers[i])
    }
const start=index-4>=0?指数-4:0
const end=index+4>this.props.initialMarkers.length
? this.props.initialMarkers.length
:索引+4
for(让i=start;i=0?索引-4:0;i this.props.initialMarkers.length
?这个.props.initialMarkers.length
:索引+4;i++){
newData.push(this.props.initialMarkers[i])
}

为什么我的第二个代码不起作用?请帮助我。

您需要在表达式中添加大括号:

// calculation works with braces preference so you will get desired output 
// same as it was with start and end variables

for (let i = 1; i <= (((index + 4) > this.props.initialMarkers.length)
  ? this.props.initialMarkers.length
  : (index + 4)); i++) {
  newData.push(this.props.initialMarkers[i])
}
//计算使用大括号首选项,因此您将获得所需的输出
//与开始和结束变量相同
for(设i=1;i this.props.initialMarkers.length)
? this.props.initialMarkers.length
:(指数+4));(i++){
newData.push(this.props.initialMarkers[i])
}
您尝试过的是给出不同的结果(根据不带大括号的数学运算符):

let index=5;//静态值
对于(设i=index-4>=0?index-4:0;i6
6.
:index+4;i++){//6是this.props.initialMarkers.length的静态值
console.log('start',i);
console.log('不带breaces的结束值:',i 6
6.
:index+4);//您的情况
console.log('end',((i 6))
6.
:(索引+4));//与布莱斯
break;//添加了break,因为它将在无限循环中运行
}
为什么我的第二个代码不起作用?请帮帮我

你的问题在于这部分

i <= index + 4 > this.props.initialMarkers.length
i this.props.initialMarkers.length
基本上有相同的so级别,除非您进行分组

i <= ( index + 4 > this.props.initialMarkers.length ?  this.props.initialMarkers.length : index + 4 );
i this.props.initialMarkers.length?this.props.initialMarkers.length:索引+4);

js引擎将执行
i您需要添加大括号

for (let i = (index - 4 >= 0 ? index - 4 : 0); i <= (index + 4 > this.props.initialMarkers.length
    ? this.props.initialMarkers.length
    : index + 4); i++) {
  newData.push(this.props.initialMarkers[i])
}
for(让i=(index-4>=0?index-4:0);i this.props.initialMarkers.length
?这个.props.initialMarkers.length
:指数+4);(i++){
newData.push(this.props.initialMarkers[i])
}

您需要在表达式中添加大括号,如
(索引-4)
问题是关于工作代码;属于代码审查时,它在工作代码中如何工作?