Using angular foreach loop to modify JSON

I have ng-repeat that 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 would like to pull out the objects and insert them into another array so that 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 orderBy on an array. Is it possible to restructure JSON to 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
    }

      

+1


source to share


2 answers


Just working through your comment to answer: -

You can do this to concatenate the arrays scattered across different months 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)

==> Gives you months in a property Year

for an array Array.prototype.map

==> You loop through an array of months and return a 2D array of days from all months. [].concat

==> returns an array of concatenated arrays. This can result in a lot of arrays, which is why we use function.apply

2D to 1D conversion.

Bin

Another simple and effective way is always looping and adding.

+2


source


Array.prototype.concat allows you to combine two arrays into one. However, this is just javascript and has nothing to do with Angular, the way to do it:



var a =  [{"day":"10","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"}];
var b = [{"day":"3","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"}];

var c = a.concat(b); // returns the concatenated array.

      

0


source







All Articles