0

With Node js Im wrote a code that returns this:

  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

The code:

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

My question is, How do i access this data. I remember in php you could do something like : $object['data'].

Is this possible with javascript.

I used stringify earlier and this was the output:

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

Out of all this data I just want to get the first and the second value. In this case it would be value: 'nike e85ba630' and '3718581.

How do i do this?

Additional information:

{ 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' } } ] } ] }
Kevin.a
  • 4,094
  • 8
  • 46
  • 82
  • 1
    Great explanation of how to acces object/array properties - https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Diego Jul 19 '17 at 11:39
  • 1
    You can access array (<<<---- which is what you have) entries by its index. `entries[0].data` – baao Jul 19 '17 at 11:39
  • 1
    @Kevin.a you can checkout my code below. It will work throughout your object. – Kamesh Jul 19 '17 at 11:52

2 Answers2

2

You can do it like this:

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);
    }
}

Additional Code you requested:

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);
Kamesh
  • 1,122
  • 9
  • 12
1

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

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

Yasin Bahtiyar
  • 2,357
  • 3
  • 18
  • 18