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' } } ] } ] }
3
  • 1
    Great explanation of how to acces object/array properties - stackoverflow.com/questions/11922383/… Commented Jul 19, 2017 at 11:39
  • 1
    You can access array (<<<---- which is what you have) entries by its index. entries[0].data Commented Jul 19, 2017 at 11:39
  • 1
    @Kevin.a you can checkout my code below. It will work throughout your object. Commented Jul 19, 2017 at 11:52

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

7 Comments

when i run node app it does nothing.
What is your exact stringified object. Can you post it?
Hi, I want the output to be a formatted like this : [ { "KEYWORD_TEXT": "red herring 9e23f4ad", "SEARCH_VOLUME": 4574730 }, { "KEYWORD_TEXT": "nike 656e95f0", "SEARCH_VOLUME": 3442386 }, Do you know how to achieve this ?
Is it fixed that the data array in your case will have only 2 objects?
Its working but in this case it only shows the values, I would like it to show it like this [ { "KEYWORD_TEXT": "red herring 9e23f4ad", "SEARCH_VOLUME": 4574730 } If this is possible
|
1

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

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

2 Comments

I get this error on the first one TypeError: Cannot read property 'data' of undefined
resultaten.entries[0].data[0].key Try this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.