Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.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 如何在jQuery中迭代动态创建的数组?_Javascript_Jquery_Oracle Jet - Fatal编程技术网

Javascript 如何在jQuery中迭代动态创建的数组?

Javascript 如何在jQuery中迭代动态创建的数组?,javascript,jquery,oracle-jet,Javascript,Jquery,Oracle Jet,我有一个从API调用创建的数组。 下面是我如何制作这个数组的- var data5 = ko.observableArray(); /*Most important thing to make the data array observable otherwise it will not show the data of the REST API*/ var arrow = []; function practiceData() { // var data = [];/**/ $.g

我有一个从API调用创建的数组。 下面是我如何制作这个数组的-

var data5 = ko.observableArray(); /*Most important thing to make the data array observable otherwise it will not show the data of the REST API*/
var arrow = [];

function practiceData() {
  // var data = [];/**/

  $.getJSON("REST API").then(function(dataset) {
    $.each(dataset, function(index, value) {
      //console.table((dataset));
      //console.log(value.change);
      data5.push(value); // PUSH THE VALUES INSIDE THE ARRAY.
      arrow.push(value.change);
      console.log("arrow", arrow);
      arrow.forEach(function(value) {
        if (value == 0) {
          $("#triangle-down-small").hide();
          $("#triangle-up-small").hide();
          console.log("rgjak")
          console.log(value);
          //  document.getElementById("navTabBar").style.visibility = "none";
        } else if (value < 0) {
          //  $("#triangle-down-small").hide();
          $("#triangle-up-small").hide();
          console.log("hcdsb")
          console.log(value);
        }
      });
    });
  });
}
JSON响应结构-

[
{
        "change": 0,
        "count": 6,
        "duration": 4,
        "prevcount": 6,
        "subcategory": "Consultancy"
}
]
对于if-else条件,我无法遍历数组。hide()函数仅适用于if条件,而不适用于else-if。 感谢您的帮助。
提前感谢。

仅供参考,请将循环移动到每个$之后。但这样的编码毫无意义。糟糕的做法

function practiceData() {
  $.getJSON("REST API").then(function (dataset) {
    $.each(dataset, function (index, value) {
      data5.push(value); // PUSH THE VALUES INSIDE THE ARRAY.
      arrow.push(value.change);
    });
    console.log("arrow", arrow);
    arrow.forEach(function (value) {
      if (value == 0) {
        console.log("rgjak");
        console.log(value);
      } else if (value < 0) {
        console.log("hcdsb");
        console.log(value);
      }
    });
  });
}
函数实践数据(){
$.getJSON(“RESTAPI”).then(函数(数据集){
$.each(数据集、函数(索引、值){
data5.push(value);//将值推送到数组中。
箭头。按下(值。更改);
});
控制台日志(“箭头”,箭头);
箭头.forEach(函数(值){
如果(值==0){
console.log(“rgjak”);
console.log(值);
}else if(值<0){
控制台日志(“hcdsb”);
console.log(值);
}
});
});
}

数组foreach函数非常简单和基本,因此没有任何问题。 我想你的问题是你的数据结构不一致或者你的代码有逻辑问题。 您应该确保始终检查推入数组的值是否存在,并且是否为数字

e、 g

另外,给定json响应数据,[0,0,-100] if和else语句都将运行,并且两个按钮都将隐藏。
只要数组中有一个零,两个按钮都将隐藏。

有控制台错误吗?另外,你的片段有点混乱。所有这些逻辑都在同一个地方。如中所示,第二个代码段是
then()
调用的延续吗?您的第一个代码段也没有清楚地显示json响应结构。@t.niese,第一个代码段显示的是console.log(“箭头”,箭头)@Taplar,我已经编辑了代码段第一个snipplet仍然不清楚您的json响应是什么。谢谢。但问题仍然是一样的。请检查一下。
function practiceData() {
  $.getJSON("REST API").then(function (dataset) {
    $.each(dataset, function (index, value) {
      data5.push(value); // PUSH THE VALUES INSIDE THE ARRAY.
      arrow.push(value.change);
    });
    console.log("arrow", arrow);
    arrow.forEach(function (value) {
      if (value == 0) {
        console.log("rgjak");
        console.log(value);
      } else if (value < 0) {
        console.log("hcdsb");
        console.log(value);
      }
    });
  });
}
if (!value || typeof value.change === 'undefined') {
    return ;
}