使用 angular foreach 循环重新排列 JSON [英] Using angular foreach loop to rearrange JSON

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

问题描述

我有一个 ng-repeat,它返回如下对象数组:

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

我想拉出对象并将它们推入另一个数组,以便它们的格式如下:

<预><代码>[{"day":"10","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"},{"day":"3","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"}]

目标是在数组上使用 orderBy.是否可以将JSON重组为这种格式,然后访问数据?

以下是我的观点供参考:

<div ng-repeat="cal in calendar[n].year | filterKey:month"><p>{{cal}}</p>

我的 JSON 格式:

<代码>{"_id" : ObjectId("53f252537d343a9ad862866c"),年" : {十二月":[],十一月" : [],十月":[],九月" : [],八月" : [],七月" : [{天":21","title" : "你","摘要" : "你",描述":好的","_id" : ObjectId("53f252537d343a9ad862866d")}],六月" : [],可能" : [],四月" : [],行进" : [],二月":[],一月" : []},__v":0},{"_id" : ObjectId("53f252537d343a9ad862866c"),年" : {十二月":[],十一月" : [],十月":[],九月" : [],八月" : [],七月" : [{天":3","title" : "你","摘要" : "你",描述":好的","_id" : ObjectId("53f252537d343a9ad862866d")}],六月" : [],可能" : [],四月" : [],行进" : [],二月":[],一月" : []},__v":0}

解决方案

只是详细说明我的评论来回答:-

您可以通过这种方式将分散在各个月份的数组合并为 1 个数组.

//obj 有作为输入的数组结果var temp = [];var result = temp.concat.apply(temp,obj.map(function(itm){//获取源数组中保存年份的每个对象.return temp.concat.apply(temp, Object.keys(itm.year).map(function(key){//为每个yeah获取月份并将其展平返回 itm.year[key];//获取每年每个月的日期数组}));}));

Object.keys(obj.year) ==> 将您的财产 Year 中的月份提供给数组Array.prototype.map ==> 您传入月份数组并返回所有月份的二维天数数组.[].concat ==> 返回连接数组的数组.它可以接受多个数组作为参数,因此我们使用 function.apply 方便地将 2D 转换为 1D.

Bin

其他简单且性能有效的方法总是循环和添加.

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.

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

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