【问题标题】:How to I get the values of the keys in this javascript object inside the arrays?如何获取数组内此 javascript 对象中键的值?
【发布时间】:2017-02-15 16:26:24
【问题描述】:

我需要键 country_code 和 language_code 的值。

我试过了

sourceLanguageArray[0].$.country_code;
sourceLanguageArray[0].$.language_code;

我得到错误:

console.log(sourceLanguageArray[0].$.country_code);
            ^
ReferenceError: sourceLanguageArray is not defined

这是数据结构的代码和控制台日志的样子:

source_language (includes country_code and language_code)
const sourceLanguageArray = _.map(ontomlClass, 'source_language');

console.log(sourceLanguageArray);
// => [ [ { '$': [Object] } ], [ { '$': [Object] } ],[ { '$': [Object] } ] ]
// => [ { '$': [Object] } ]

console.log(sourceLanguageArray[0]);
// => [ { '$': { country_code: 'US', language_code: 'en' } } ]

【问题讨论】:

    标签: javascript arrays object key


    【解决方案1】:

    var sourceLanguageArray = [[ { '$': { country_code: 'US', language_code: 'en' } } ]];
    
    console.log("sourceLanguageArray[0]:", sourceLanguageArray[0]);
    
    console.log("country code:", sourceLanguageArray[0][0].$.country_code);
    console.log("language code:", sourceLanguageArray[0][0].$.language_code);

    【讨论】:

      【解决方案2】:

      试试这个:

      sourceLanguageArray.forEach(function(item){
          console.log(item[0]['$'][country_code]);
          console.log(item[0]['$'][language_code]);
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多