Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON对象转换为特定Javascript数组或JSON对象_Javascript_Arrays_Json_Javascript Objects - Fatal编程技术网

JSON对象转换为特定Javascript数组或JSON对象

JSON对象转换为特定Javascript数组或JSON对象,javascript,arrays,json,javascript-objects,Javascript,Arrays,Json,Javascript Objects,我有一个JSON对象。我们称之为vardataFetched var dataFetched = { "status": "ok", "count": 4, "count_total": 4, "pages": 1, "posts": [ { "id": 57, "type": "keyword", "slug": "crime-scene-investigation-csi", "url": "ht

我有一个JSON对象。我们称之为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": {

      }
      }
      ]
}
我想重新排列此结果以链接类别标题:

dataFetched.posts[i].categories[0].title 
职位名称:

dataFetched.post[i].title
这样,每个类别都会显示与其相关的所有帖子标题。我希望我的对象(无论是多维数组还是其他对象)能够检索与该类别相关的所有文章标题

也许是这样的:

   [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;
        }

    }
}
//对数组的唯一值进行排序的函数
函数排序_唯一(arr){
arr=arr.sort(函数(a,b){返回a*1-b*1;});
var-ret=[arr[0]];
对于(var i=1;i对于(i=0;i您显示的输出示例是错误的,在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;
}

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

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]}

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

您编写了哪些代码来解决此问题?请包括您已经尝试过的内容,我们不是来帮您做这些事情的:只是为了帮助您。如果可能的话,我还建议您在服务器上创建过滤器(PHP、SQL)?这对用户来说会更好,因为下载的数据会大大减少。谢谢,我添加了我尝试的内容