2017-07-19 95 views
0

随着节点的js林写道,返回此代码:JavaScript中访问特定对象

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。

我字符串化之前使用,这是输出:

{ 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' } } ] } ] } 
+1

如何存取权限对象/数组属性很好的解释 - https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Diego

+1

您可以访问阵列(<<< ----这是你所拥有的)条目的索引。 'entries [0] .data' – baao

+1

@ Kevin.a你可以在下面查看我的代码。它会在整个对象中工作。 – Kamesh

回答

2

你可以这样说:

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); 
+0

当我运行节点应用程序它什么都不做。 –

+0

什么是您确切的字符串化对象。你可以发布吗? – Kamesh

+0

编辑我的帖子与整个对象 –

1

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

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

+0

我得到这个错误的第一个TypeError:无法读取未定义的属性“数据” –

+0

'resultaten.entries [0] .data [0] .key'试试这个 –