简体   繁体   中英

JavaScript for-loop separate values and return it

My Code for the array:

var values = [];
    let arrayTraceLength = resp.Result.details.length
        let p = 0
        for (p = 1; p < 20; p++) {
        console.log(p)
        for ( var y = 0; y < arrayTraceLength; y++) {
          if (resp.Result.details[y].key == 'InternetGatewayDevice.TraceRouteDiagnostics.RouteHops.' + p + '.HopHostAddress') {
          console.log(' HopHost===> ' + resp.Result.details[y].value)
            values += resp.Result.details[y].value     
        }
          }
        }
        return values; 


  

Want to display data to the user but these values should be separate array return in html . For instance, the 00004 should be 0 0 0 0 4 the first 0 is the first RouteHop. Second 0 is the Second RouteHop etc. The return values is what I use to return the values to the html.

values is an array so use Array.prototype.push() not the string concatenation operator += . Also the code you posted is incomplete, you can only return values from within a function but I assume you want to do something like this:

 var values = [];
        let p = 0
        for (p = 1; p < 20; p++) {
        console.log(p)
        for ( var y = 0; y < arrayTraceLength; y++) {
          if (resp.Result.details[y].key == 'InternetGatewayDevice.TraceRouteDiagnostics.RouteHops.' + p + '.HopHostAddress') {
          console.log(' HopHost===> ' + resp.Result.details[y].value)
            values.push(resp.Result.details[y].value);    
        }
          }
        }
        return values; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM