0

I have object

$scope.postData = {
  'pmu.messages.message': $scope.upd.message,
  'pmu.received.id': $scope.upd.atomByReceivedBy.id,
};

and in scope there is $scope.ImageList which contains image path array

[
  { img: 'a.jpg', smallimg: 'b.jpg', smpath: 'c.jpg' },
  { img: 'a1.jpg', smallimg: 'b1.jpg', smpath: 'c1.jpg' },
];

I want to add these array value to $scope.postData field property value as

$scope.postData = {
   'pmu.messages.message': $scope.upd.message,
   'pmu.received.id': $scope.upd.atomByReceivedBy.id,
   'pmu.image[0].img':
   'pmu.image[0].smallimg':
   'pmu.image[0].smimg':
   'pmu.image[1].img':
   'pmu.image[1].smallimg':
   'pmu.image[1].smimg':
}

How to achieve this?

0

2 Answers 2

1

Iterate $scope.ImageList and use Bracket Notation to create properties.

Here in the example, I have used just one property

$scope.ImageList.forEach(function(element, index){
    $scope.postData['pmu.image[' + index +'].img'] = element.img;
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can iterate the array with Array.forEach, and then extract the key & value of each image using Object.entries.

$scope = {
  upd: {
    message: '',
    atomByReceivedBy: {
      id: ''
    }
  },
  ImageList: [{
      img: 'a.jpg',
      smallimg: 'b.jpg',
      smpath: 'c.jpg'
    },
    {
      img: 'a1.jpg',
      smallimg: 'b1.jpg',
      smpath: 'c1.jpg'
    },
  ]
}

$scope.postData = {
  'pmu.messages.message': $scope.upd.message,
  'pmu.received.id': $scope.upd.atomByReceivedBy.id,
};

$scope.ImageList.forEach((img, index) => {
  Object.entries(img).forEach(([key, value]) => {
    $scope.postData[`pmu.image[${index}].${key}`] = value;
  });
});

console.log($scope.postData);

1 Comment

Working properly

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.