Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

javascript - Using angular foreach loop to rearrange JSON

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
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...