2

So I've to different objects, getting data from different sources. I need to convert them into a single object so that I can ng-repeat them.

object1 = {
"key1":"value one",
"key2":"value two"
}

object2 = {
"key1":"value three",
"key2":"value four"
}

What I need is

object3 = [{
"key1":"value one",
"key2":"value two"},
{
"key1":"value one",
"key2":"value two"}
}];

I tried angular.merge but it only works if the keys are different. In my case, keys are gonna be the same, only data is gonna change.

Can anyone help append object1 to object2 to create something like object3 which is object1+object2.

I tried converting them to json string & concatenating them but it didn't work. Array contact function is not working either.

4
  • Read How to Ask. Then create MCVE. This is very simple, but you need to fix your question. Besides, you need to learn JavaScript. Commented Dec 26, 2015 at 11:06
  • what you want to make is an array of objects. look up Arrays. Commented Dec 26, 2015 at 11:10
  • Please do some basic research before asking questions on StackOverflow. See JS Tutorial - JavaScript Array Methods. Commented Dec 26, 2015 at 11:53
  • Guys, I understand you care about community guidelines and keeping stackoverflow's content of good quality, but I've been fighting with this question over a week, doing research as much as possible. Since I didn't get answer, that's why I asked question. Commented Dec 26, 2015 at 12:02

1 Answer 1

3

This is just vanilla javascript. You want to make an array of from the objects.

var object1 = {
  "key1": "value one",
  "key2": "value two"
}

var object2 = {
  "key1": "value three",
  "key2": "value four"
}

var object3 = []; // empty array

// add the other objects to the array
object3.push(object1);
object3.push(object2);

if your objects are already arrays and you just want to concatenate them, you can use the concat function on arrays.

var object1 = [{
  key: 'abc',
  value: 1
}];
var object2 = [{
  key: 'cbd',
  value: 2
}];

// use the concat function to concatenate the arrays
var object3 = object1.concat(object2);

You can find more about arrays by reading the documentation found here.

If your objects are firebaseArray objects they have their own API. More can be found here.

Sign up to request clarification or add additional context in comments.

7 Comments

Alright, your solution works when object1 & 2 are like {"key":"value"}. But in my case, I'm getting object1 as [{"key":"value"}]. So resulting object3 is getting something like this: [[{"key":"value1"}],["key":"value 2"]]
@Vineonardo so object1 and object2 are already arrays? and you just want to concatenate them?
that's what I'm trying but it's not working. It says concat is not a function.
@Vineonardo I added an example on how to concatenate arrays. You should take some time and read the documentation for them too. The link is in the answer.
I'll read that, thanks. I'm a js noob so understanding core concepts is taking time. Idk why it says concat won't work since concat not a function.
|

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.