无法获取数组内的信息

我试图获取数组中的信息但不能,每次都返回一个未定义的语句。


那是数组,它在变量“RESULTADO”中


debug   {"recordType":"customrecord5",

         "id":"1",

         "values":{

           "CUSTRECORD4.custrecord6":[{"value":"11",

                                       "text":"11 CP BUSINESS : Empresa Teste"}],

           "CUSTRECORD4.custrecord5":[{"value":"7",

                                       "text":"LASER"}]}}

代码:


for (var i = 0; i < resultado.length; i++){

    var valores = resultado[i];

    log.debug(valores);

    var dados = valores['value']; // NAO ESTOU CONSEGUINDO PERCORRER A VARIAVEL DADOS 

    log.debug(dados);

    for (var u = 0; u < dados.length; u++){

        valor = dados[u];

        log.debug(valor);

    }


}

我需要获取索引“值”内的信息,但无法进入...


天涯尽头无女友
浏览 98回答 2
2回答

30秒到达战场

这是因为您拥有的数据不是数组,而是对象。好吧,JS 对象也被称为“关联数组”,但它们有很多不同的行为。它们由“KVP”键值对{KEY:VALUE}构成要迭代一个对象,您必须使用不同类型的循环,如 for/in 循环。let resultado = {&nbsp; &nbsp;"recordType": "customrecord5",&nbsp; &nbsp;"id": "1",&nbsp; &nbsp;"values": {&nbsp; &nbsp; &nbsp; "CUSTRECORD4.custrecord6": [{"value": "11", "text": "11 CP BUSINESS : Empresa&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Teste"}],&nbsp; &nbsp; &nbsp; "CUSTRECORD4.custrecord5": [{"value": "7", "text": "LASER"}]&nbsp; &nbsp;}};for (let KEY in resultado) {&nbsp; &nbsp;console.log(KEY); // recordType, id, values&nbsp; &nbsp;console.log(resultado[KEY]); // customrecord5, 1, {...}}或者你可以把它变成数组let resultadoArr = Object.entries(resultado);console.log(resultadoArr);// [ [ 'recordType', 'customrecord5' ],//&nbsp; &nbsp;[ 'id', '1' ],//&nbsp; &nbsp;[ 'values',//&nbsp; &nbsp; &nbsp;{ 'CUSTRECORD4.custrecord6': [Object],//&nbsp; &nbsp; &nbsp; &nbsp;'CUSTRECORD4.custrecord5': [Object] } ] ]还有一个建议,使用“let”和“const”而不是“var”。这是当今的新标准。我希望我有所帮助
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript