How to get all properties values of a JavaScript Object (without knowing the keys)?
By using a simple for..in
loop:
for(var key in objects) {
var value = objects[key];
}
How to get all properties values of a Javascript Nested Objects (without knowing the keys)?
The easiest way is probably with a recursive function. You can do that like this in a modern engine:
const errorData = {"Message":"The request is invalid.","ModelState":{"account.Contact.PrimaryPhone":["Primary Phone is required.","'Primary Phone' should not be empty."]}}
const errors = (function flattenValues( obj ) {
return Object.values( obj ).reduce(
( values, value ) => values.concat( typeof value === "object" ? flattenValues( value ) : value )
, [ ] );
} )( errorData );
console.log( errors );
How to access an object without knowing its name
You could try to use the Object.keys()
(also Object.values()
) to get keys (or values) of an object without any knowledge of it's data.
Try the following
let data = { 11791146: {m_serverQuery: {name: 'sample'}, m_key: 11791146}};
console.log(Object.keys(data)[0]);
console.log(Object.values(data)[0]);
console.log(Object.values(data)[0].m_serverQuery);
console.log(Object.values(data)[0].m_key);
How to access a javascript object value without knowing the key
data = {
a : 2,
b : 3
}
for(var propName in data) {
if(data.hasOwnProperty(propName)) {
var propValue = data[propName];
// do something with each element here
}
}
How do I access properties of a javascript object if I don't know the names?
Old versions of JavaScript (< ES5) require using a for..in
loop:
for (var key in data) {
if (data.hasOwnProperty(key)) {
// do something with key
}
}
ES5 introduces Object.keys and Array#forEach which makes this a little easier:
var data = { foo: 'bar', baz: 'quux' };
Object.keys(data); // ['foo', 'baz']
Object.keys(data).map(function(key){ return data[key] }) // ['bar', 'quux']
Object.keys(data).forEach(function (key) {
// do something with data[key]
});
ES2017 introduces Object.values
and Object.entries
.
Object.values(data) // ['bar', 'quux']
Object.entries(data) // [['foo', 'bar'], ['baz', 'quux']]
Accessing a JavaScript's object property without knowing that property name
Object.keys(m)[0]
should return the first enumerable property name in the object m
.
So if m = {"who": "Arthur"};
then m[Object.keys(m)[0]]
will be "Arthur"
.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys
Alternatively: Object.values(m)[0]
. See Object.values
Get the value of an object with an unknown single key in JS
Object.keys might be a solution:
Object.keys({ dbm: -45}); // ["dbm"]
The differences between for-in and Object.keys is that Object.keys returns all own key names and for-in can be used to iterate over all own and inherited key names of an object.
As James Brierley commented below you can assign an unknown property of an object in this fashion:
var obj = { dbm:-45 };
var unkownKey = Object.keys(obj)[0];
obj[unkownKey] = 52;
But you have to keep in mind that assigning a property that Object.keys returns key name in some order of might be error-prone.
Related Topics
Why Do Regex Constructors Need to Be Double Escaped
JavaScript - Sort Array Based on Another Array
Canvascontext2D Drawimage() Issue [Onload and Cors]
How to Access Post Form Fields in Express
Working With $Scope.$Emit and $Scope.$On
How to Check If a String "Startswith" Another String
Build Tree Array from Flat Array in JavaScript
How to Check For "Undefined" in JavaScript
JavaScript: Client-Side Vs. Server-Side Validation
What Does "JavaScript:Void(0)" Mean
Difference Between Null and Undefined in JavaScript
How to Detect Changes in Location Hash
Can Scrapy Be Used to Scrape Dynamic Content from Websites That Are Using Ajax
What's the Difference Between "Array()" and "[]" While Declaring a JavaScript Array
"Cross Origin Requests Are Only Supported For Http." Error When Loading a Local File