使用javascript

时间:2017-07-21 08:27:28

标签: javascript json

我使用Node处理API。通过节点I,我可以获得数据列表。 当我使用Stringify我得到这样的东西:

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

我现在的问题是。我想要键值对数据,例如:

[
  {
    "KEYWORD_TEXT": "red herring 9e23f4ad",
    "SEARCH_VOLUME": 4574730
  },
  {
    "KEYWORD_TEXT": "nike 656e95f0",
    "SEARCH_VOLUME": 3442386
  },
  etc...
]

在我的代码中,我尝试了下一个:

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

})

这个输出接近我想要但不完全正确。这是我得到的:

{ data:
   [ { key: 'KEYWORD_TEXT', value: [Object] },
     { key: 'COMPETITION', value: [Object] },
     { key: 'SEARCH_VOLUME', value: [Object] } ] }

正如您所看到的那样,键是beign显示的,但值仍然是一个对象。我想上面的数据是相同的,除了我想要显示的值。

2 个答案:

答案 0 :(得分:0)

您可以使用[].map()创建包含结果的新数组。这是一个例子......

var newResult = resultaten.entries.map(function(entry) {
    var result = {};

    entry.data.forEach(function (datum) {
        result[datum.key] = datum.value.value;
    })

    return result;
})

你的价值在那里,我认为console.log只是显示[Object]插入netsted对象的详细值。

答案 1 :(得分:0)

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)

我从你的问题中获取了json数据的一部分。请检查你是否正在寻找这个。