0

I have 2 object arrays and will use those value on checkbox for view. I want to merge 2 obj arrays to make it one json object file. help me please

 $scope.fruits = [
{ name: 'apple',    selected: true },
{ name: 'orange',   selected: false },
{ name: 'pear',     selected: true },
{ name: 'naartjie', selected: false }
];


$scope.color = [
    { name: 'red',    selected: true },
    { name: 'green',   selected: false },
    { name: 'orange',     selected: true },
    { name: 'yellow', selected: false }
];

And the view,

<label ng-repeat="fruit in fruits" class="checkbox-inline">
<input type="checkbox" name="selectedFruits[]" value="{{fruitName}}" ng-model="fruit.selected"> {{fruit.name}}</label>
<label ng-repeat="color in colors" class="checkbox-inline">
<input type="checkbox" name="selectedColors[]" value="{{colorName}}" ng-model="color.selected"> {{color.name}}</label>

And the output i want like below:

[{
    "name": "apple",
     "color": "red",
    "selected": true
  },
{
"name": "orange",
 "color": "yellow",
"selected": true},
{
"name": "pear",
 "color": "green",
"selected": true}]
3
  • 1
    Those two particular arrays are mismatched when you look at your output. For example array1/object1 does match with array2/object1, but the next one array1/object2 seems to match with array2/object3 in your output. How are you supposed to know which colour matches with which fruit? Commented Oct 6, 2017 at 9:14
  • Some key should be there both objects. Commented Oct 6, 2017 at 9:50
  • @rrd thanks. What i want is like your example. For example array1/object1 does match with array2/object1, but the next one array1/object2 seems to match with array2/object3 in your output. User must select Fruit first after that choose the color. No need to think about for matches or not. What i want is after user clicked i need to merge the arrays with json format. Commented Oct 6, 2017 at 10:10

2 Answers 2

0

you can use map function for this.

 var fruits = [
{ name: 'apple',    selected: true },
{ name: 'orange',   selected: false },
{ name: 'pear',     selected: true },
{ name: 'naartjie', selected: false }
];


var color = [
    { name: 'red',    selected: true },
    { name: 'green',   selected: false },
    { name: 'orange',     selected: true },
    { name: 'yellow', selected: false }
];

var arr = fruits.map((o,i)=>{
   o.color = color[i].name;
   return o;
});

console.log(arr)

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

2 Comments

i don't want to be map. I want merge based on user choose.
For example array1/object1 does match with array2/object1, but the next one array1/object2 seems to match with array2/object3 in your output. User must select Fruit first after that choose the color. No need to think about for matches or not. What i want is after user clicked i need to merge the arrays with json format.
0

Use Angular merge enter link description here

var mergedObject = angular.extend(object1, object2);

And manual way by ignoring null and missing values would be

this

1 Comment

NOP.For example array1/object1 does match with array2/object1, but the next one array1/object2 seems to match with array2/object3 in your output. User must select Fruit first after that choose the color. No need to think about for matches or not. What i want is after user clicked i need to merge the arrays with json format.

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.