loop throught all members of an object in javascript [duplicate] - javascript

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

Related

How to get values from a nested array in javascript? [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 4 years ago.
I have an array like let a = [[[1,2], [3,[1,2,3]]], [2,3]] and want to access the elements using a method/way to return the values like : 12312323 or [1,2,3,1,2,3,2,3]
how can I approach the solution in javascript/nodeJS?
thanks
You can use Array.flat()
let a = [[[1,2], [3,[1,2,3]]], [2,3]]
let op = a.flat(Infinity)
console.log(op)
You can check browser compatibility here compatibility

How to read Object? [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 5 years ago.
Say i have object like
var a = {"user":
{'average':
{'score':4
}
}
}
How can I read object value using its keys
Say I have user Object with me and have key "average.score" can I get the value directly?
a.user["average.score"];
//Coming as undefined
a.user["average"]["score"]
// Working as expected : 4
I have the key of "average.score" all together with me want to get the value of score how can I do it directly without splitting the key.
Use a.user["average"].score
var a = {"user":
{'average':
{'score':4
}
}
}
console.log(a.user["average"].score);

Javascript beginner. Get all properties of javascript object [duplicate]

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.

How to show an object value in for loop in java script [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 8 years ago.
I need to print all datas in an object in a for loop but I don't know how to access to their index.
For example I have an object like this (this is an example ,but the real one is very big)
a = { name : richard ,last_name : stallman };
I can do this :
cnosole.log(a.name);
cnosole.log(a.last_name);
But the object is too big.How can I do this?
Our you could do it like this:
a = { name : 'richard' ,last_name : 'stallman' };
for( key in a){
console.log(a[key]);
}
You can convert an object to a string with JSON.stringify.
console.log(JSON.stringify(a));
should print something like:
{"name": "richard", "last_name": "stallman"}

Swap values of keys in JSON array [duplicate]

This question already has answers here:
Swap value of two properties on object(s)
(3 answers)
Closed 8 years ago.
I have the following JSON. I need to swap SortId
Like I have this,
[{"CategoryId":1,"Name":"Worktable","SortId":1}
,{"CategoryId":2,"Name":"Bf ","SortId":2}]
After swaping their 'SortId' I need
[{"CategoryId":1,"Name":"Worktable","SortId":2}
,{"CategoryId":2,"Name":"Bf ","SortId":1}]
Please tell me how to do it through JavaScript.
var tmp = a[0].SortId;
a[0].SortId = a[1].SortId;
a[1].SortId = tmp;
jsFiddle

Categories

Resources