var repeatelem = function(elem, n){
// returns an array with element elem repeated n times.
var arr = [];
for (var i=0; i<n; i++) {
(function(newValue, index){
console.log(index)
Object.defineProperty(newValue, 'reasonId', {
value:index,
configurable:true
});
arr = arr.concat(elem);
}(elem, i))
};
return arr;
};
var res = repeatelem({a:1},3)
console.log(res)
I was excepting the reasonId
to be 0,1,2.
not sure, why there is a problem here even after using closures the value of reasonId
set to last index value.
Excepted :
[{a:1,'reasonId':0 }, {a:1,'reasonId':1 }, {a:1,'reasonId':2 }]