Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...