I declared and and assigning values to an array in the following way but it says error at this line attributes.categoryKey: '2051' unexpected token '.'.Can someone help me.Thanks.
var arr2 = [ {'attributes': {'categoryKey': '' },'value': '' } ]
$.each(value, function (i, v) {
arr2.push({ attributes.categoryKey: '2051', 'value': v.name });
You cannot do this directly
arr2.push({ attributes.categoryKey: '2051', 'value': v.name });
as here attributes is not defined yet so .categoryKey is invalid hence you need to define its as further properties of object as you did initially
It needs to be as :
var arr2 = [ {'attributes': {'categoryKey': '' },'value': '' } ]
var value = [{name:"name1"}]
$.each(value, function (i, v) {
arr2.push({ attributes: {categoryKey: '2051'}, 'value': v.name });
})
console.log(arr2);
See this fiddle https://jsfiddle.net/7ggcjjh7/
You used wrong syntax:
var object = {key: 'value'};
the key must be clearly and we cannot use some expression in it.
But I have a suggestion to by pass this:
var a = {'value': v.name };
a[attributes.categoryKey] = '2051';
Finally:
arr2.push(a);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.