duan19850312 2015-08-13 15:59
浏览 32
已采纳

使用javascript在Kimonolabs中为json输出添加类别

I have Json output file in Kimonolabs lookin like this

{
"name": "text",
"results": {
"collection1": [
  {
    "title": {
      "href": "link",
      "text": "text"
    },
    "description": "text",
    "author": "text",
    "index": 1,
    "url": "link"
  },etc.
}]}}

I want to add a variable 'category' to the variables in the collection1 array, and make the value of that category 'food' for each of the array items. But struggling all day to get it working. Any suggestions?

What I tried is:

function transform (data){

//create shortcut for collection variable
var collection = data.results.collection1;

//function to add category
function addcat(line){
line[category] ='food';
return row;
}

//loop through variables;
for (var i=0; i < collection.length; i++){
[collection] = [collection].map(addcat);

return data;
}
  • 写回答

1条回答 默认 最新

  • douxing2156 2015-08-13 16:15
    关注
    for (var i = 0; i < object.results.collection1.length; i++) {
        object.results.collection1[i].category = 'food';
    };
    

    http://jsfiddle.net/6yf7z47b/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?