This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 6 years ago.
I am trying to create an Array in Javascript with some key:value pair. My initial array looks likes this:
var data = {
isPublic : true,
distance : 500,
noOfPlayer : 10
}
Now there is a checkbox, upon which upon checking I need to add more data to array. Say I check a box and need to update the array with its value and make the array look like this:
var data = {
isPublic : true,
distance : 500,
noOfPlayer : 10,
typePlay: 'amateur'
}
Can this be done, because I tried creating object with Key:value and pushing it to the array, but it gives and output as below:
var data =
[
{
isPublic : true,
distance : 500,
noOfPlayer : 10,
},
{
typePlay: 'amateur'
}
]
Please suggest.
Thanks,
Ayush
Use object["property"] = value or object.property = value to add a new property with its value
var data = {
"isPublic" : true,
"distance" : 500,
"noOfPlayer" : 10
};
data["typePlay"] = "amateur";
data.typePlay2 = "amateur";
console.log(data);
Related
This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 3 years ago.
I have a json string which starts like this:
{ "0" :
{"Jquery77768" :
{"nodeData":
{"id":32, "name": "Bob"}
----
I need to get the value which is there in the id key.
I tried to do something like this:
var obj = JSON.parse(myJsonString);
var myID = obj[0].Jquery77768.nodeData.id;
but this does not work. Also the second node name Jquery77768 is dynamic and will change every time.
How can I get the value of the id field?
Since you mentioned dynamic key names (Jquery77768), It will be better to get the Object.values. (Assuming one key as in data).
var myID = Object.values(obj["0"])[0].nodeData.id;
What about a general function ? But suppose order of keys may not be same everywhere...
var obj = '{ "0" : {"Jquery77768" : {"nodeData": {"id":32, "name": "Bob"} }}}';
obj = JSON.parse(obj);
console.log(goDownNth(obj[0], 1).nodeData.id);
console.log(goDownNth(obj[0], 1).nodeData.id === goDownNth(goDownNth(goDownNth(goDownNth(obj, 1), 1), 1), 1));
console.log(goDownNth(goDownNth(goDownNth(goDownNth(obj, 1), 1), 1), 2));
function goDownNth (obj, n) {
for (var a in obj) {
if (!--n) return obj[a];
}
}
This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 3 years ago.
I send a get request to the api wichh returns data under form of
{
"message": "<SUCCESS>",
"result": [
{
"i": 1,
},
{
"i": 2,
},
{
"i": 3,
} ]
}
Then in javascript (angular component.ts) I want to create an array ob objects but after pushing the objects in the array every array contains the data of the last object:
.subscribe(
data => {
type MyArrayType = Array<UserOpinion>;
let array: MyArrayType = [];
let cont: CustomInterface = {
i: ''
}
data.result.forEach(function(entry) {
cont.i = entry["i"];
array.push(cont);
console.log(cont);
//right value is shown
})
//console.log(array)
//array contains the connect number of objects but the objects are set on the same value: "i" = 3
Any ideas why this happens? Thank you
This should do it:
.subscribe(
data => {
type MyArrayType = Array<UserOpinion>;
let array: MyArrayType = [];
data.result.forEach(function(entry) {
array.push(entry);
});
}
);
You dont need cont just for copying the current object to another, you can use the current loop element directly.
This question already has answers here:
Does JavaScript guarantee object property order?
(13 answers)
Sorting a JavaScript object by property name
(7 answers)
Closed 6 years ago.
In my Javascript application, I have an array with few properties var sampleArray = ["Number_Info", "Additional_Ids","Additional_Ids_2","_summary"];Now I need to convert this array into an object containing properties in the same order, as shown below :
var sampleObject = {
"Number_Info" : {},
"Additional_Ids" : {},
"Additional_Ids_2" : {},
"_summary" : {}
}
I tried the following :
var orderingObj = {};
for(var i=0; i < sampleArray.length; i++ ){
orderingObj[sampleArray[i]] = {}
}
But here I am getting object properties in different order.
var orderingObj = {
"Additional_Ids_2" : {},
"Additional_Ids" : {},
"Number_Info" : {},
"_summary" : {}
};
How can I fix it? From earlier posts the solutions were there to sort by names but here I have a specific order in which I want to order my properties.
This question already has answers here:
JavaScript associative array to JSON
(5 answers)
Closed 7 years ago.
I'm creating a JS object like this
var eleDetailsTop = new Array();
var j = 0;
var id = "ele"+j;
eleDetailsTop[id] = {id: id, size : "40%", sizeLabel : 12, type : "image", title : "Image"};
Now, I'm trying to convert this object to a JSON...
var fields = JSON.stringify(eleDetailsTop);
So, my problem is, this only gives an empty result.
Here is what I got when I debugged with FireBug
As you can see there is another object called wrappedJSobject inside this. If you checked inside it, you can see another wrappedJSobject as so on...
Why is this ? Why is this weird ?
You're creating an array and assigning it alphanumeric property.
Try this:
var j = 0;
var id = "ele"+j;
eleDetailsTop[j] = {id: id, size : "40%", sizeLabel : 12, type : "image", title : "Image"};
EDIT: If you do want to use id as property - defined eleDetailsTop as object:
var eleDetailsTop = {};
If you do:
var eleDetailsTop = {};
Then you can assign properties like
eleDetailsTop[id] = {}
But if you really need the array... that won't work because there's no associative arrays in js technically (they're objects).
<script>
var test = {};
test['iam1234'] = {};
console.log(test);
</script>
This question already has answers here:
JSON find in JavaScript
(7 answers)
Closed 7 years ago.
Assume I have an Array of jQuery objects. I would like to search in array and find out such a key exists are not.
NOTE: The objects are not of same pattern
Ex:
a = [ { "chart_type" : 4},
{ "x_axis" : { "field_type" : 5, "name" : "priority" } },
{ "y_axis" : { "field_type" : 3, "name" : "created_at" }}
]
I would like to search from the above, if key such as "chart_type" exists or not. If yes, I would like to get the value.
a.find("chart_type").value // Need something like this
I tried the following.
jQuery.grep(chart_hash, function(key,value){
if(key == "chart_type")
return value
});
However, the above doesn't work. Please help.
Use jQuery.each
$.each(chart_hash, function(index,value){
$.each(value,function(key,val){
if(key == "chart_type")
return val
});
})
})