简体   繁体   English

在 Javascript 中,如何使用“push”函数定位数组条目?

[英]in Javascript how do i target an array entry with "push" function?

i've been struggling with this, would appreciate help.我一直在努力解决这个问题,希望得到帮助。

i'm looking to update the value of "v.low" to like 6000 (manually) outside of the loop at the last index position in the array (last index = latest timestamp, so its current time).我希望在数组的最后一个索引位置(最后一个索引 = 最新时间戳,所以它的当前时间)在循环外将“v.low”的值更新为 6000(手动)。 But i can't target the entry properly in the array with JS.但是我无法使用 JS 在数组中正确定位条目。

here is an example - but the below is sitting in a loop where "v" is associated to the row in array这是一个例子 - 但下面是一个循环,其中“v”与数组中的行相关联

  chartList[container]['option'].series[0].data.push([
    v.open,
    v.close,
    v.low,    ----< want to update its value here to 6000
    v.high
  ]);

//i'm trying it this way, but its obviously wrong, want to update low @ last index position //我正在尝试这种方式,但显然是错误的,想要更新低@最后一个索引位置

chartList[container]['option'].series[0].data[ arraylength-1 ].low=6000; chartList[container]['option'].series[0].data[ arraylength-1 ] .low =6000; ----> also tried this: chartList[container]['option'].series[0].data[ arraylength-1 ].push('low',6000); ----> 也试过这个:chartList[container]['option'].series[0].data[ arraylength-1 ].push('low',6000);

//i essentially want to update the value of "low" at the last index in array "chartList[container]['option'].series[0].data" //我本质上想更新数组“chartList[container]['option'].series[0].data”中最后一个索引处的“low”值

Here is the code这是代码


// List all data candle
  $.each(dataParsed.candles, function(k, v){ vListChart.push(v.close);   });



  if(jQuery.inArray(dataParsed.candles[dataParsed.candles.length - 1].date, chartList[container]['option'].xAxis[0].data) === -1){

  //  showAlert('we have new data');        

    chartList[container]['option'].series[0].data = chartList[container]['option'].series[0].data.slice(0, -4);
    chartList[container]['option'].series[1].data = chartList[container]['option'].series[1].data.slice(0, -4);
    chartList[container]['option'].xAxis[0].data = chartList[container]['option'].xAxis[0].data.slice(0, -4);

    chartList[container]['data_candles'] = chartList[container]['data_candles'].slice(0, -4);
    chartList[container]['data_candles'] = $.merge(chartList[container]['data_candles'], dataParsed.candles);

    chartList[container]['data'] = chartList[container]['data'].slice(0, -4);
    chartList[container]['data'] = $.merge(chartList[container]['data'], vListChart);

    $.each(dataParsed.candles, function(k, v){
      chartList[container]['option'].xAxis[0].data.push(v.date);
      chartList[container]['option'].series[0].data.push([
        v.open,
        v.close,
        v.low,
        v.high
      ]);

      if(k == dataParsed.candles.length - 1){
        chartList[container]['option'].series[1].data.push(v.open);
      } else {
        chartList[container]['option'].series[1].data.push(v.close);
      }

    });

/// this is the candle formation /// 这是蜡烛的形成

echo json_encode([
  'error' => 0,
  'candles' => $listCandles,
  'current_price' => $Coin->_getPrice(),
  'notification_list' => $listNotification,
  'order_list' => $listOrder,
  'internal_order' => $internalOrderList,
  'currency' => $CryptoApi->_getCurrency()
]);
chartList[container]['option'].series[0].data[ arraylength-1 ][2]=6000

请注意,这不会影响 v 引用的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JavaScript push()将一组值推入数组? - How do I push a group of values to an array with Javascript push()? 如何将新的复杂条目添加到javascript数组? - How do I add a new complex entry to a javascript array? 在 JavaScript 中,如何编写接收命名元素以有条件地推入数组的函数? - In JavaScript, how do I write a function that receives a named element to conditionally push into an array? 我如何使用递归 function 在 javascript 中推送数组 - How could I use the Recursion function to push array in javascript 如何将元素推入JavaScript中的嵌套数组中? - How do I push elements into a nested array in javascript? 如何在Javascript中将每个元素数组推入2个属性? - How do I push to a 2-attribute per element array in Javascript? 在javascript中使用split函数后如何推送变量? - How do i push a variable after using split function in javascript? javascript:如何将单选值推送到另一个函数? - javascript: how do i push a radio value to another function? 如何将数组推送到递归 javascript 函数内的数组,以便在完成后可以使用它? - How do I push arrays to array inside a recursive javascript function so that I can use it when it's done? Javascript数组如何为数组中的每一行做一个功能 - Javascript array how do i do a function for each line in array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM