Prashant Shukla Prashant Shukla - 6 months ago 9
AngularJS Question

Assign dynamic index in array of object in Angularjs

If I have array of object in such a way.

data[0].name= 'Prashant Shukla';
$scope.getColumn[i].Field = 'name';


Using above I want to assign
Prashant
in
$scope.getColumn[i].value
in dynamic way. I have try like this

$scope.getColumn[i].value = data[0].+$scope.getColumn[i].Field;


but this give me
Uncaught SyntaxError: Unexpected token +


I can get
Prashant Shukla
by
data[0].name
but How can I get exactly same by using
$scope.getColumn[i].Field
in place of
name
index ?

how can I resolve this error and assign value dynamically?

thanks ahead.

Answer

you can use as like as given below:

data[0].name= 'Prashant Shukla';
$scope.getColumn[i].Field = 'name';

console.log( data[0][ $scope.getColumn[i].Field ] );  // 'Prashant Shukla'