【问题标题】:Javascript access certain ObjectsJavascript 访问某些对象
【发布时间】:2017-07-19 11:37:31
【问题描述】:

我用 Node js 写了一个返回这个的代码:

  entries:
   [ { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] },
     { data: [Object] } ] } null

代码:

targetingIdeaService.get({selector: selector}, function (error, result) {
    var resultaten = result;
    console.log(resultaten);
}) 

我的问题是,我如何访问这些数据。我记得在 php 中你可以这样做:$object['data']

这可以用 javascript 实现吗?

我之前使用了 stringify,这是输出:

{ data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike e85ba630' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '3718581' } } ] },

在所有这些数据中,我只想获得第一个和第二个值。在这种情况下,它将是值:'nike e85ba630' 和 '3718581。

我该怎么做?

附加信息:

{ totalNumEntries: 700,
  entries:
   [ { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'red herring 9e23f4ad' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '4574730' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike 656e95f0' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '3442386' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'red herring 2bb32682' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '2641524' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike d4b589f6' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '4778937' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike f10afc91' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '4693893' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'red herring 8c856a44' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '6021387' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike d63a9577' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '5794059' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike 4f21151' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '4579430' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike 64b804ed' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '6190489' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'red herring f4e7622a' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '6469058' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'red herring f59a9380' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '5207137' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike 2b886e20' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '4274335' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike a5874a43' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '5289335' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike 49914ecd' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '4007549' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike 38d6b6c2' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '2965677' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike 6fc88361' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '4060879' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'red herring f8f4ee95' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '4771620' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike 4fafb3a0' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '5501852' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'nike e85ba630' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '3718581' } } ] },
     { data:
        [ { key: 'KEYWORD_TEXT',
            value:
             { attributes: { 'xsi:type': 'StringAttribute' },
               'Attribute.Type': 'StringAttribute',
               value: 'red herring 59bd66ea' } },
          { key: 'SEARCH_VOLUME',
            value:
             { attributes: { 'xsi:type': 'LongAttribute' },
               'Attribute.Type': 'LongAttribute',
               value: '4529457' } } ] } ] }

【问题讨论】:

  • 很好地解释了如何访问对象/数组属性 - stackoverflow.com/questions/11922383/…
  • 您可以通过索引访问数组(entries[0].data
  • @Kevin.a 你可以在下面查看我的代码。它将在您的整个对象中起作用。

标签: javascript node.js


【解决方案1】:

你可以这样做:

var res = resultaten.entries;
for(var i = 0; i < res.length; i++){
    var data = res[i].data;
    for(var j = 0; j < data.length; j++){
        var valueObject = data[j].value;
        console.log(valueObject.value);
    }
}

您要求的附加代码:

var res = resultaten.entries;
var arr = [];
for(var i = 0; i < res.length; i++){
    var data = res[i].data;
    var obj = {};
    for(var j = 0; j < 2; j++){
        var valueObject = data[j].value;
        console.log(valueObject.value);
        if(j === 0){
            obj.KEYWORD_TEXT = valueObject.value;
        }
        else if(j === 1){
            obj.SEARCH_VOLUME = valueObject.value;
        }
        arr.push(obj);
    }
}
console.log('ARRAY', arr);

【讨论】:

  • 当我运行节点应用程序时它什么也不做。
  • 您的确切字符串化对象是什么。可以发一下吗?
  • 嗨,我希望输出格式如下: [ { "KEYWORD_TEXT": "red herring 9e23f4ad", "SEARCH_VOLUME": 4574730 }, { "KEYWORD_TEXT": "nike 656e95f0", "SEARCH_VOLUME": 3442386 },你知道如何实现吗?
  • 你的数据数组只有2个对象的问题是固定的吗?
  • 它的工作,但在这种情况下它只显示值,我希望它像这样显示它 [ { "KEYWORD_TEXT": "red herring 9e23f4ad", "SEARCH_VOLUME": 4574730 } 如果这是可能
【解决方案2】:

resultaten.entries[0].data[0].key

resultaten.entries[0].data[0].value.value

【讨论】:

  • 我在第一个 TypeError 上收到此错误:无法读取未定义的属性“数据”
  • resultaten.entries[0].data[0].key 试试这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
  • 2017-02-23
  • 2013-05-28
  • 1970-01-01
  • 2016-01-31
  • 1970-01-01
相关资源
最近更新 更多