Im working with an API using Node. With node I'm able to get a list of data. When i Use Stringify i get something like this :
{ 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' } } ] },
My problem now is. I want Key-value pairs of data for example :
[
{
"KEYWORD_TEXT": "red herring 9e23f4ad",
"SEARCH_VOLUME": 4574730
},
{
"KEYWORD_TEXT": "nike 656e95f0",
"SEARCH_VOLUME": 3442386
},
etc...
]
In my code I have tried the next :
targetingIdeaService.get({selector: selector}, function (error, result) {
var resultaten = result;
console.log(resultaten.entries[0]);
})
The output of this was close to what i wanted but not exactly. Here is what I'm getting:
{ data:
[ { key: 'KEYWORD_TEXT', value: [Object] },
{ key: 'COMPETITION', value: [Object] },
{ key: 'SEARCH_VOLUME', value: [Object] } ] }
As you can see the key is beign shown but the value is still an object. I would like the above data to be the same except i want the value to be shown too.
You can use [].map() to create new array with the results. Here is an example...
var newResult = resultaten.entries.map(function(entry) {
var result = {};
entry.data.forEach(function (datum) {
result[datum.key] = datum.value.value;
})
return result;
})
Your value is there, I think console.log
just show [Object]
instend of the detail value for netsted objects.
var ls = { "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" } }] }] }; // stringified var newList = []; for (var i = 0; i < ls.entries.length; i++) { var obj = ls.entries[i]; var newObj = {}; for (var j = 0; j < obj.data.length; j++) { var contnt = obj.data[j]; newObj[contnt.key] = contnt.value.value; } newList.push(newObj); } console.log(newList)
I have taken a part of json data from your question. please check wether you are looking for this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.