采用了棱角分明的foreach循环重新排列JSON [英] Using angular foreach loop to rearrange JSON

查看:231
本文介绍了采用了棱角分明的foreach循环重新排列JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NG重复返回类似下面的对象数组:

<$p$p><$c$c>[{\"day\":\"10\",\"title\":\"day\",\"summary\":\"summary\",\"description\":\"ok\",\"_id\":\"53f25185bffedb83d8348b22\"}]
[{天:3,称号:天,总结:摘要,说明:OK,_ ID:53f25185bffedb83d8348b22}]

我想有拉出对象并将其推到另一个阵列,使他们的格式是这样的:

  [
{天:10,称号:天,总结:摘要,说明:OK,_ ID:53f25185bffedb83d8348b22},
    {天:3,称号:天,总结:摘要,说明:OK,_ ID:53f25185bffedb83d8348b22
}]

的目的是在阵列上使用ORDERBY。是否有可能到JSON重组为这种格式,然后访问数据?

下面是我的参考视图:

 &LT; D​​IV CLASS =calDynamic数据-NG-重复=N在[] |范围:100&GT;
&LT; D​​IV NG重复=CAL日历[N] .year | filterKey:一个月&GT;    &所述p为H.; {{校准}}&下; / P&GT;
&LT; / DIV&GT;
&LT; / DIV&GT;

我的JSON格式:

  {
_id:的ObjectId(53f252537d343a9ad862866c),
年 : {
    十二月:[],
    十一月 : [],
    十一:[],
    九·一:[],
    八月 : [],
    七月:[
        {
            天时:21,
            头衔:U,
            摘要:U
            说明:OK,
            _id:的ObjectId(53f252537d343a9ad862866d)
        }
    ]
    六一:[],
    五一:[],
    四五:[],
    三八:[],
    二月:[],
    一月 : []
},
__v:0
},
{
    _id:的ObjectId(53f252537d343a9ad862866c),
    年 : {
        十二月:[],
        十一月 : [],
        十一:[],
        九·一:[],
        八月 : [],
        七月:[
            {
                天时:3
                头衔:U,
                摘要:U
                说明:OK,
                _id:的ObjectId(53f252537d343a9ad862866d)
            }
        ]
        六一:[],
        五一:[],
        四五:[],
        三八:[],
        二月:[],
        一月 : []
    },
    __v:0
    }


解决方案

只是阐述我的评论回答: -

您可以做到这一点的方式分散在不同月份的阵列合并1阵列。

  // OBJ有数组结果是输入
变种临时= [];
VAR的结果= temp.concat.apply(温度,obj.map(功能(ITM){//获取源数组持有一年中的每个对象。
  返回temp.concat.apply(温度,Object.keys(itm.year).MAP(功能(键){//获取每月为每个耶和压平
       返回itm.year [关键] //取得天阵列每年每一个月的
  }));
}));

对象。键(obj.year) ==>会给你你的财产的几个月年度到一个数组
Array.prototype.map ==>您通过几个月数组中,并从所有几个月找回天的二维数组。
[]。CONCAT ==>级联阵列的回报数组。它可以在多个阵列参数,所以我们使用<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply\"相对=nofollow> function.apply 方便地2D转换成一维。

其他的简单,性能有效的方式总是循环并增加对。

I have an ng-repeat which returns arrays of objects like the following:

[{"day":"10","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"}]
[{"day":"3","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"}]

I'd like to have pull out the objects and push them into another array so they are formatted like this:

[
{"day":"10","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"},
    {"day":"3","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"
}]

The goal is to use an orderBy on the array. Is it possible to restructure the JSON into this format and then access the data?

Here is my view for reference:

<div class="calDynamic" data-ng-repeat="n in [] | range:100">
<div ng-repeat="cal in calendar[n].year | filterKey:month">

    <p>{{cal}}</p>
</div>
</div>

My JSON format:

{
"_id" : ObjectId("53f252537d343a9ad862866c"),
"year" : {
    "December" : [],
    "November" : [],
    "October" : [],
    "September" : [],
    "August" : [],
    "July" : [ 
        {
            "day" : "21",
            "title" : "u",
            "summary" : "u",
            "description" : "ok",
            "_id" : ObjectId("53f252537d343a9ad862866d")
        }
    ],
    "June" : [],
    "May" : [],
    "April" : [],
    "March" : [],
    "February" : [],
    "January" : []
},
"__v" : 0
},
{
    "_id" : ObjectId("53f252537d343a9ad862866c"),
    "year" : {
        "December" : [],
        "November" : [],
        "October" : [],
        "September" : [],
        "August" : [],
        "July" : [ 
            {
                "day" : "3",
                "title" : "u",
                "summary" : "u",
                "description" : "ok",
                "_id" : ObjectId("53f252537d343a9ad862866d")
            }
        ],
        "June" : [],
        "May" : [],
        "April" : [],
        "March" : [],
        "February" : [],
        "January" : []
    },
    "__v" : 0
    }

解决方案

Just elaborating my comment to answer:-

You can do this way to merge the arrays scattered across various month to 1 array.

//obj has the the array result that is the input
var temp = [];
var result = temp.concat.apply(temp,obj.map(function(itm){ //Get each object in the source array that hold the year.
  return temp.concat.apply(temp, Object.keys(itm.year).map(function(key){ //Get Month for each yeah and flatten it out
       return itm.year[key]; //Get day array for each of the month in each year
  }));
}));

Object.keys(obj.year) ==> Will give you the months in your property Year to an array Array.prototype.map ==> You pass in the months array and get back 2D array of days from all of months. [].concat ==> returns array of concatenated arrays. It can take multiple arrays are argument, so we use function.apply conveniently to convert 2D to 1D.

Bin

Other simple and performance effective way always is loop through and add upon.

这篇关于采用了棱角分明的foreach循环重新排列JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆