0

I have a result of array which contain multiple objects and i need to merge this result set in single array using java-script. I have tried with many of JavaScript functions but no success. Also this one have response generated after using array.push.

Response

[

  [Object {
      category_of_student = "Freshman", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
    },
    Object {
      category_of_student = "Freshman", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
    },
    Object {
      category_of_student = "Ophomore", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
    },
    26 more...
  ],

  [Object {
    category_of_student = "Junior", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
  }]

]

Code

var yourCat = item.category_of_student;

var optionVal = [];
for (i = 0; i < yourCat.length; i++) {

  var res = ($filter('filter')(item, {
    category_of_student: yourCat[i]
  }));
  optionVal.push(res);

}

Require Result

[

  Object {
    category_of_student = "Freshman", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
  },
  Object {
    category_of_student = "Freshman", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
  },
  Object {
    category_of_student = "Ophomore", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
  },
  26 more...,

  Object {
    category_of_student = "Junior", your_school = "(BA Bs)Bachelor of Scien...Business Administration", college = "Business of Unit", more...
  }

]
5
  • How about using concat? Commented Jan 19, 2016 at 14:32
  • I already try this but for my case its not working @nikhil Commented Jan 19, 2016 at 14:33
  • Shouldn't category_of_student = "Ophomore", be category_of_student: "Ophomore",? Commented Jan 19, 2016 at 14:34
  • Looks like you are trying to flatten out the array. Commented Jan 19, 2016 at 14:35
  • @MinusFour yah i just need to combine this after array.push Commented Jan 19, 2016 at 14:36

2 Answers 2

2

You can quickly transform this with reduce and concat:

arr.reduce(function(a, b){  return a.concat(b); });

Example:

var arr = [
  [{
    category_of_student : "Freshman",
    your_school : "(BA Bs)Bachelor of Scien...Business Administration",
    college : "Business of Unit"
  },
  {
    category_of_student : "Freshman",
    your_school : "(BA Bs)Bachelor of Scien...Business Administration",
    college : "Business of Unit"
  },
  {
    category_of_student : "Freshman",
    your_school : "(BA Bs)Bachelor of Scien...Business Administration",
    college : "Business of Unit"
  }],
  [{
    category_of_student : "Junior",
    your_school : "(BA Bs)Bachelor of Scien...Business Administration",
    college : "Business of Unit"
  }]
]

var transformed = arr.reduce(function(a, b){ return a.concat(b); });

before.innerHTML = JSON.stringify(arr, null, '\t');
results.innerHTML = JSON.stringify(transformed, null, '\t');
<h1>Before</h1>
<pre id="before"></pre>
<h1>After</h1>
<pre id="results"></pre>

3
  • how i use this function of my case Commented Jan 19, 2016 at 14:42
  • I'll add an example. Commented Jan 19, 2016 at 14:43
  • Thankyou, You save my day. Commented Jan 19, 2016 at 14:59
0

Try:

var result = [];
for (var i=0; i<Response.length; ++i) {
  for (var j=0; j<Response[i].length; ++j) {
    result.push(Response[i][j]);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.