This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 5 years ago.
let's suppose I have the following object:
k = {
name : 'Sam',
age : 20,
interests : " ... "
friends: " ... "
};
How to get all the objects properties without using the Object.keys() function in the ouptup format:
['name', 'age', 'interests' ...]
Note: don't mark my question as duplicate and provide a link to an answer with the Object.keys() function
var keys = [];
for (var key in k){
keys.push(key);
}
For more complicated objects than your example, you may want to check k.hasOwnProperty(key) in that loop before adding it.
Related
This question already has answers here:
Add key value pair to all objects in array
(12 answers)
Closed 5 years ago.
Im new to spring, I convert a object as json to use in jsp page. The json object looks like following.
[
{"year":2014,"average":115.5},
{"year":2016,"average":0.0},
{"year":2017,"average":8.28},
{"year":2018,"average":0.0}
]
I need to add another key value pair ( "color" : value ) to each object like given below.
[
{"year":2014,"average":115.5,"color":1},
{"year":2016,"average":0.0,"color":2},
{"year":2017,"average":8.28,"color":3},
{"year":2018,"average":0.0,"color":4}
]
Now I passed to page, how to add this key value pair to each objects in javascript?
Add to your existing object like so:
var yourNewArray = yourOriginalArray.map(function(obj) {
var me = Object.assign({}, obj);
me.color = 'yourNewColorValue';
return me;
});
This question already has answers here:
Javascript deep copying object [duplicate]
(3 answers)
Closed 6 years ago.
I have a javascript object on a page that contains some info. I need to loop through the object and copy it to another object.
Here is my starting code
var p_tag = {};
var new_tag = {};
p_tag.abc='abc';
p_tag.def='def';
p_tag.ghi='ghi';
for (var key in p_tag) {
if (p_tag.hasOwnProperty(key)) {
console.log(key + "'s favorite fruit is " + p_tag[key]);
}
}
I need to take the values from p_tag and copy them to new_tag as another object.
I may not know what keys are available on each page so looking for something that can go through all available p_tags and copy them to new_tag.
Thanks,
The easiest way to do a deep copy of an object:
var copy = JSON.parse(JSON.stringify(obj));
So it basically turns your object into a string and then back into an object again with zero references to the original.
Just assign all of the old values to the new values.
for (var key in p_tag) {
if (p_tag.hasOwnProperty(key)) {
console.log(key + "'s favorite fruit is " + p_tag[key]);
new_tag[key] = p_tag[key];
}
}
This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 8 years ago.
I am trying to figure out a way to add field names and values to an object.
for example I hav ethe following...
$scope.product = {
"Requirements": [
{
"OriginPostcode": '',
"BearerSize": 100,
"BandwidthRequired": 10
}
]
}
And I want to add two more names and values...
"Term": 36,
"Quantity": 1
I know the push() function is for arrays. What do you uses for objects?
Many thanks
You can use baces passing index like an array:
$scope.product["Requirements"][0]["Term"] = 36
$scope.product["Requirements"][0]["Quantity"] = 1
you can also do in this way
$scope.product["Requirements"][0].Term = 36
$scope.product["Requirements"][0].Quantity = 1
and also...
$scope.product.Requirements[0].Term = 36
$scope.product.Requirements[0].Quantity = 1
What do you need to understand is how to go through a javascript object using braces [] or point . Take a look at this link
This question already has answers here:
How to keep the sequence in javascript Map?
(2 answers)
Does JavaScript guarantee object property order?
(13 answers)
Closed 8 years ago.
I have a JSON array and I am trying to sort it by value. The problem I am having is that I am not able to keep the JSON structure with my sorting.
Here is the JSON array:
{
caffeineoverdose: '2517',
workhardplayhard: '761277',
familia: '4633452'
}
I would like something like this:
{
familia: '4633452',
workhardplayhard: '761277',
caffeineoverdose: '2517
}
Here is everything you need.
Like i said already in the comments you can't sort an object.. but you can put it into an array and display the results.
var array=[],obj={
caffeineoverdose:'2517',
workhardplayhard:'761277',
familia:'4633452'
};
for(a in obj){
array.push([a,obj[a]])
}
array.sort(function(a,b){return a[1] - b[1]});
array.reverse();
DEMO
http://jsfiddle.net/GB23m/1/
You could convert it into an array of objects:
[{ name: 'caffeineoverdose', number: '2517' }, {name: 'workhardplayhard', number: '761277'}, {name: 'familia', number: '4633452'}]
and then sort by number
array.sort(function(a,b){
return a.number - b.number;
}
);
That's not JSON, and it's not an array. It's a regular JavaScript object, and you cannot impose an ordering on the properties of an object.
If you want to maintain the order of your elements, you need an array (again, this isn't JSON, it is JavaScript):
[ [ 'familia', '4633452'] ,
[ 'workhardplayhard', '761277'],
[ 'caffeineoverdose', '2517']
]
This question already has answers here:
How to loop through a plain JavaScript object with the objects as members
(28 answers)
Closed 9 years ago.
{
"USA":[{"country":"Albuquerque (ABQ)"},
{"country":"Allentown (ABE)"}
],
"Canada":{"country":"Calgary (YYC)"},
"Hawaii":{"country":"Honolulu International Apt (HNL)"}
}
This is my object I need to loop it through so i can get all the values like this
USA
Albuquerque (ABQ)
Allentown (ABE)
Canade
Calgary (YYC)
For the solution you can check out this demo.
DEMO
You can loop Object using for(k in obj)
for(k in obj){
var value = obj[k];
}
I have created a jsfiddle for you at http://jsfiddle.net/5eM4q/
The way you access the data using
data[obj].country
is incorrect