在另一个对象中抓取特定对象

时间:2015-12-15 17:38:26

标签: javascript arrays json sorting

我有这个js对象

{
  "2015": {
    "11": {
      "10": {
        "Family & Friends\nGames": {
          "c": 2
        },
        "Collectible\nGames": {
          "c": 1
        },
        "Logic Games\n": {
          "c": 1
        },
        "Kids Games\n": {
          "c": 1
        }
      },      
    },
    "Family & Friends\nGames": {
      "c": 9
    },
    "Collectible\nGames": {
      "c": 2
    },
    "Logic Games\n": {
      "c": 4
    },
    "Kids Games\n": {
      "c": 6
    },
    "Classic Games\n": {
      "c": 3
    },
    "Preschool\nGames": {
      "c": 5
    }
  },
  "meta": {
    "id": [
      "[CLY]2",
      "[CLY]7",
      "[CLY]6",
      "[CLY]1",
      "[CLY]4",
      "[CLY]3"
    ],
    "segments": [
      "id",
      "title"
    ],
    "title": [
      "Family & Friends\nGames",
      "Collectible\nGames",
      "Logic Games\n",
      "Kids Games\n",
      "Classic Games\n",
      "Preschool\nGames"
    ]
  }
}

我试图在“meta”上面减去与“title”下的键匹配的相同对象。我使用ruby使用以下代码完成了这个

     results = JSON.parse(resp.body)
     data = results["2015"]
     title = results["meta"]["title"]
     alg = data.slice(*title).to_a
     info = alg.sort_by { |k,v| v["c"] }

所以我尝试将对象转换为数组,并从那里切片匹配其他数组名称标题的所有内容,最后根据它们的“c”值对信息进行排序。

这似乎对我没什么用,所以我试图用[]和内部键的名称从对象中获取数据,但由于数组“title”下的每个名称都有\ n让我不得不添加一个额外的\来访问它。我希望这是动态的,每当我在“title”

下有不同的名字时就不必修改它

我需要这样的东西https://jsfiddle.net/7chntms2/6/

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

这是你想要实现的目标吗?

var obj = {
  "2015": {
    "11": {
      "10": {
        "Family & Friends\nGames": {
          "c": 2
        },
        "Collectible\nGames": {
          "c": 1
        },
        "Logic Games\n": {
          "c": 1
        },
        "Kids Games\n": {
          "c": 1
        }
      },
    },
    "Family & Friends\nGames": {
      "c": 9
    },
    "Collectible\nGames": {
      "c": 2
    },
    "Logic Games\n": {
      "c": 4
    },
    "Kids Games\n": {
      "c": 6
    },
    "Classic Games\n": {
      "c": 3
    },
    "Preschool\nGames": {
      "c": 5
    }
  },
  "meta": {
    "id": [
      "[CLY]2",
      "[CLY]7",
      "[CLY]6",
      "[CLY]1",
      "[CLY]4",
      "[CLY]3"
    ],
    "segments": [
      "id",
      "title"
    ],
    "title": [
      "Family & Friends\nGames",
      "Collectible\nGames",
      "Logic Games\n",
      "Kids Games\n",
      "Classic Games\n",
      "Preschool\nGames"
    ]
  }
}

var keys = obj["meta"]["title"];
var newObj = [];

for (var i = 0; i < keys.length; i++) {
  newObj.push({
    name: keys[i],
    content: obj["2015"][keys[i]]
  });
}

newObj.sort(function(a, b) {
  if (a.content.c < b.content.c) return -1;
  if (a.content.c > b.content.c) return 1;
  return 0;
});

console.log(newObj);

答案 1 :(得分:0)

示例小提琴:https://jsfiddle.net/wv2kbcyn/

基本上,如果该JSON对象的名称是数据:

var meta = data["meta"];
var titles = meta["title"];
var logicgames = titles[2];

您还可以循环标题:

for(var key in titles)
{
    var title = titles[key];
    console.log(title); // "Family & Friends\nGames", ...
}

所以titles现在是一个数组。