JSON Object 转换成特定的 Javascript 数组或 JSON Object

JSON Object Transformation into specific Javascript Array or JSON Object

所以我有这个 JSON Object。我们称它为 var dataFetched

var dataFetched = {
    "status": "ok",
    "count": 4,
    "count_total": 4,
    "pages": 1,
    "posts": [
    {
      "id": 57,
      "type": "keyword",
      "slug": "crime-scene-investigation-csi",
      "url": "http://keyjargon.com/keyword/crime-scene-investigation-csi/",
      "status": "publish",
      "title": "Crime Scene Investigation (CSI)",
      "title_plain": "Crime Scene Investigation (CSI)",
      "content": "",
      "excerpt": "",
      "date": "2015-11-07 05:01:51",
      "modified": "2015-11-07 05:01:51",
      "categories": [
        {
          "id": 8,
          "slug": "law",
          "title": "Law",
          "description": "",
          "parent": 0,
          "post_count": 1
        }
      ],
      "tags": [

      ],
      "author": {
        "id": 1,
        "slug": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "admin",
        "url": "",
        "description": ""
      },
      "comments": [

      ],
      "attachments": [

      ],
      "comment_count": 0,
      "comment_status": "closed",
      "custom_fields": {

      }
    },
    {
      "id": 50,
      "type": "keyword",
      "slug": "fx",
      "url": "http://keyjargon.com/keyword/fx/",
      "status": "publish",
      "title": "FX",
      "title_plain": "FX",
      "content": "",
      "excerpt": "",
      "date": "2015-11-05 10:07:17",
      "modified": "2015-11-05 10:22:10",
      "categories": [
        {
          "id": 3,
          "slug": "business",
          "title": "Business",
          "description": "",
          "parent": 0,
          "post_count": 2
        }
      ],
      "tags": [

      ],
      "author": {
        "id": 1,
        "slug": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "admin",
        "url": "",
        "description": ""
      },
      "comments": [

      ],
      "attachments": [

      ],
      "comment_count": 0,
      "comment_status": "closed",
      "custom_fields": {

      }
    },
    {
      "id": 48,
      "type": "keyword",
      "slug": "common-core",
      "url": "http://keyjargon.com/keyword/common-core/",
      "status": "publish",
      "title": "Common CORE",
      "title_plain": "Common CORE",
      "content": "",
      "excerpt": "",
      "date": "2015-11-05 10:06:40",
      "modified": "2015-11-07 04:58:06",
      "categories": [
        {
          "id": 2,
          "slug": "technology",
          "title": "Technology",
          "description": "",
          "parent": 0,
          "post_count": 3
        }
      ],
      "tags": [

      ],
      "author": {
        "id": 1,
        "slug": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "admin",
        "url": "",
        "description": ""
      },
      "comments": [

      ],
      "attachments": [

      ],
      "comment_count": 0,
      "comment_status": "closed",
      "custom_fields": {

      }
    },
    {
      "id": 46,
      "type": "keyword",
      "slug": "api",
      "url": "http://keyjargon.com/keyword/api/",
      "status": "publish",
      "title": "API",
      "title_plain": "API",
      "content": "",
      "excerpt": "",
      "date": "2015-11-05 10:06:19",
      "modified": "2015-11-05 10:21:47",
      "categories": [
        {
          "id": 2,
          "slug": "technology",
          "title": "Technology",
          "description": "",
          "parent": 0,
          "post_count": 3
        }
      ],
      "tags": [

      ],
      "author": {
        "id": 1,
        "slug": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "admin",
        "url": "",
        "description": ""
      },
      "comments": [

      ],
      "attachments": [

      ],
      "comment_count": 0,
      "comment_status": "closed",
      "custom_fields": {

      }
      }
      ]
}

我想将此结果重新排列为 link 类别标题:

dataFetched.posts[i].categories[0].title 

到Post标题:

dataFetched.post[i].title

以便每个类别显示与其相关的所有帖子标题。我希望我的 object(无论是 multi-demmensional 数组还是另一个 object)能够检索与该类别相关的所有 Posts 标题。

也许是这样的:

   [Category1: {Post_titleA, PostTitleB, PostTitleC}, Category2: {PostTileF, PostTileX}, etc ] where each category can retrieve all its posts.( The format does not matter as long the Object with Category title X can retrieve all posts titles that belong to it ).

如何在 Javascript 中执行此操作?结果变量不是静态的,但其格式与此处发布的相同。

这是我到目前为止尝试过的方法。

  // Function to sort unique values of an array
  function sort_unique(arr) {
      arr = arr.sort(function (a, b) { return a*1 - b*1; });
      var ret = [arr[0]];
      for (var i = 1; i < arr.length; i++) { // start loop at 1 as element 0 can never be a duplicate
        if (arr[i-1] !== arr[i]) {
            ret.push(arr[i]);
        }
     }
     return ret;
 }

 //Define two arrays to be used for categories and Keywords
 var keywords = [];
 var industries = [];

  //Fill up the categories(Industries) array and the keywords one
  for ( var i = 0, iLen = dataFetched.count; i < iLen; i++) {

    keywords[i] = dataFetched.posts[i].title;
    industries[i] = dataFetched.posts[i].categories[0].title;
  }

  // Sort and eliminate duplication of category and keyword names
  keywords = sort_unique(keywords);
  industries =  sort_unique(industries);

  // Now time for trouble: Define a multi-dimmensional array that links each category/industry to its keywords **This is where I AM STUCK**
  ind = new Array;
  for(i=0; i<industries.length;i++){
    ind[i] = new Array;
  }     
  for(i=0;i<dataFetched.count;i++){
    ind[i][0]= dataFetched.posts[i].categories[0].title;        
    for(j=0;j<dataFetched.count;j++){
        var count  = ind[i].length;
        if(ind[i][0] == dataFetched.posts[j].categories[0].title){
            ind[i][count] = dataFetched.posts[j].title;
        }

    }
}

你给出的输出示例是错误的,在JS中没有像

这样的对象
[Category1: {Post_titleA, PostTitleB, PostTitleC}, Category2: {PostTileF, PostTileX}, etc ]

你能得到的最相似的东西是像这样的 JSON 对象:

{
  "Category1" : ["Post_titleA", "PostTitleB", "PostTitleC"],
  "Category2" : ["PostTileF", "PostTileX"],
  //etc..
}

为了实现这一点,您可以使用以下函数:

function getTitlesByCategory (json) {
    var result = {}
    json.posts.map(function (post) {
        post.categories.map(function (category) {
            result[category.title] = result[category.title] || [];
            result[category.title].push(post.title);
        });
    });

    return result;
}

可以创建 object 类别。因此,所有条目都可以通过类别名称访问,您无需对它们进行排序以获得唯一的标题:

var posts = dataFetched.posts;
var cat = {};

posts.forEach(
    function(p) {
        p.categories.forEach(            
            function(c) {
                if (!cat[c.title])
                    cat[c.title] = [];
                cat[c.title].push(p.title);
            });
    });

console.log(cat);

示例的输出:

Object {Law: Array[1], Business: Array[1], Technology: Array[2]}

每个类别标题都是此 object 中的一个键,帖子数组是这些键的值。