Reputation: 4980
I would like to add a key->value pair beginning of the a javascript JSON array like so :
var temp = new Array();
temp = {
['LABEL_ID': 10, 'LABEL_TYPE': A3],
['LABEL_ID': 17, 'LABEL_TYPE': A6]
}
what I want to do :
var temp = new Array();
temp = {
'PAPER': A80
['LABEL_ID': 10, 'LABEL_TYPE': A3],
['LABEL_ID': 17, 'LABEL_TYPE': A6]
}
could you please help me?
EDIT here is the my code:
// returns paper type from selected grid line
var ppt = Ext.getCmp('labelType').getValue();
// returns selected grid rows
var rows = Ext.getCmp('labelGrids').getSelectionModel().getSelection();
// here is my array that I want to add ppt variable value to existing array, then I will convert this to a JSON array
for (var i = 0; i < count; i++)
{
prints[i] = {'LABEL_ID': rows[i].data.LABEL_ID, 'LABEL_TYPE':rows[i].data.LABEL_TYPE}
}
The reason is to adding ppt
value beginning of the array just I need ones this record in server side. Therefore, I don't want to repeat in all lines!
Upvotes: 2
Views: 258
Reputation: 12683
Well, I hope you meant this script in your question:
And to answer your edited question, A simple way is to wrap it in another object:
var temp = new Array();
temp = [
{"LABEL_ID": 10, "LABEL_TYPE": "A3"},
{"LABEL_ID": 17, "LABEL_TYPE": "A6"}
]
// wrap temp in another object;
var obj = {};
obj.PAPER = A80;
obj.Data = temp;
//And convert to json.
console.log(JSON.stingify(obj));
Output:
{"PAPER":"A80","Data":[{"LABEL_ID":10,"LABEL_TYPE":"A3"},{"LABEL_ID":17,"LABEL_TYPE":"A6"}]}
Hope this helps. Calling the function will add the required proptery to your objects
var temp = new Array();
temp = [
{"LABEL_ID": 10, "LABEL_TYPE": "A3"},
{"LABEL_ID": 17, "LABEL_TYPE": "A6"}
]
addPaperProperty(temp, 'PAPER', ['AA','BB']);
function addPaperProperty(currentArray, newPropertyField, newPropertyArr){
var newArray = [];
var i=0;
$(temp).each(function(){
this[newPropertyField] = newPropertyArr[i++];
newArray.push(this);
});
console.log(newArray);
return newArray;
}
And the result will be
[{LABEL_ID: 10, LABEL_TYPE: "A3", PAPER: "AA"},
{LABEL_ID: 17, LABEL_TYPE: "A6", PAPER: "BB"}]
Upvotes: 2
Reputation: 47182
If you're so inclined I would say this is the way to go for you.
You're javascript syntax is invalid as it is.
You want to add "label"-objects to your Array and furthermore youre assigning temp
to a new object overwriting the Array you just created.
If you wanna add to the array you have to use the method .push()
Like this:
labelArray.push({'LABEL_ID': '10', 'LABEL_TYPE': 'A3'});
labelArray.push({'LABEL_ID': '17', 'LABEL_TYPE': 'A6'});
Otherwise this might the a pointer in the right direction
var labelArray = new Array({'LABEL_ID': 10, 'LABEL_TYPE': A3},
{'LABEL_ID': 17, 'LABEL_TYPE': A6});
var paper = {
papertype : A80,
labels : labelArray
}
But its neither good nor is it clean but it works for you.
Upvotes: 2
Reputation: 66388
This is not an array. The type of such a variable is known as hashtable as it defines key/value pairs.
Some important points before the actual answer:
So, to make your code valid you can have such a thing:
temp = {
'PAPER': 'A80',
'LABELS': [{'LABEL_ID': 10, 'LABEL_TYPE': 'A3'},
{'LABEL_ID': 17, 'LABEL_TYPE': 'A6'}]
};
This will add ordinary key/value pair then a key with the value being array of hashtables.
To access specific label you can have such code:
var id = temp["LABELS"][1]["LABEL_ID"];
Keep in mind that JavaScript is case sensitive so case is important in the key names, meaning temp["paper"]
will return undefined.
Upvotes: 1
Reputation: 72867
Your objects appear to be invalid all over the place:
The outer brackets should be square: [ ]
, if you really want it to be an array, and the inner ones curly { }
, since those are objects.
Also, unless A3
and A6
are varialbes you've defined before, you'll need to enclose them in apostrophes, since they're strings:
var temp = [
{'LABEL_ID': 10, 'LABEL_TYPE': 'A3'},
{'LABEL_ID': 17, 'LABEL_TYPE': 'A6'}
]
Now, if you want that added property in your array, you're probably best off doing it like this:
var temp = [
{'PAPER': 'A80'},
{'LABEL_ID': 10, 'LABEL_TYPE': 'A3'},
{'LABEL_ID': 17, 'LABEL_TYPE': 'A6'}
]
Or, make temp
a object, instead:
var temp = {
'PAPER':'A80',
'labels':[
{'LABEL_ID': 10, 'LABEL_TYPE': 'A3'},
{'LABEL_ID': 17, 'LABEL_TYPE': 'A6'}
]
}
(In objects, you always have a key:value
relation, in arrays, you never have that.
Upvotes: 1
Reputation: 341
Hi this is a valid JSON Object
temp = {
'PAPER': A80, 'LIST' : {
['LABEL_ID': 10, 'LABEL_TYPE': A3],
['LABEL_ID': 17, 'LABEL_TYPE': A6]
}
}
Upvotes: -1