1

I am wondering how can do something like this:

Output I am looking for:

{ item_id0 : 123, quantity0: 99 , item_id1 : 124, quantity1: 50 }

My sample code but not working:

var data = {};
angular.forEach(vm.fields, function(value, key){
    data = {
        item_id[key] : vm.selectedItem[key].id,
        quantity[key] : vm.quantity[key]
    }
})
console.log(data);
4
  • forEach((elem, id, array) => { // code }) what is it vm.fields? Commented Apr 8, 2018 at 9:03
  • 2
    Aside from it being a Bad Idea (you should have something more like [{id:123,quantity:99}, {id:124,quantity:50}]) you could probably be better off just doing data['item_id'+key] = vm.selectedItem[key].id; Commented Apr 8, 2018 at 9:03
  • Is the formatting important? You really want the fields to be named item_id0, quantity0, item_id1, quantity1 and so on? Commented Apr 8, 2018 at 9:11
  • @Artem, it is an object that needed for my input fields. @NiettheDarkAbsol, thank you for that. But when I passing it to parameter it does return null. $http.post('server/add-transaction.php', data).then(function(res){ console.log(res); }) @Schorsch, formating is not really important but I think I need it to pass in my php insert query. Commented Apr 8, 2018 at 9:27

1 Answer 1

1

You can try the following to obtain data in the format that you need, following code accesses property by its name string and gives it some value.

  $scope.property = {};

  for(i = 0; i< 10;i++){
    $scope.property['item_id'+ i] = i;
    $scope.property['quantity' + i] = i+20;
  }

Demo

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

Comments

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.