Use Object.keys:
var foo = {
'alpha': 'puffin',
'beta': 'beagle'
};
var keys = Object.keys(foo);
console.log(keys) // ['alpha', 'beta']
// (or maybe some other order, keys are unordered).
This is an ES5 feature. This means it works in all modern browsers but will not work in legacy browsers.
The ES5-shim has a implementation of Object.keys you can steal
Use Object.keys:
var foo = {
'alpha': 'puffin',
'beta': 'beagle'
};
var keys = Object.keys(foo);
console.log(keys) // ['alpha', 'beta']
// (or maybe some other order, keys are unordered).
This is an ES5 feature. This means it works in all modern browsers but will not work in legacy browsers.
The ES5-shim has a implementation of Object.keys you can steal
You can use jQuery's $.map.
var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' },
keys = $.map(foo, function(v, i){
return i;
});
jquery - javascript get key name from array of objects - Stack Overflow
Getting key of each object inside array of objects into an array: Javascript - Stack Overflow
How to get a key from array of objects by its value Javascript - Stack Overflow
Generate an Array of All Object Keys with Object.keys()
Use map() and return the first key the object. You can get keys using Object.keys()
let data = [{"ja":"大阪市"},{"en":"Osaka"}]
let res = data.map(x => Object.keys(x)[0]);
console.log(res)
If you don't want to use [0] use flatMap()
let data = [{"ja":"大阪市"},{"en":"Osaka"}]
let res = data.flatMap(x => Object.keys(x));
console.log(res)
Note: The second method will also get the other properties other than first. For example
[{"ja":"大阪市","other":"value"},{"en":"Osaka"}] //["ja","other","en"];
let data = [{"ja":"大阪市"},{"en":"Osaka"}]
let res = data.reduce((arr, o) => {
return Object.keys(o).reduce((a, k) => {
if (a.indexOf(k) == -1) a.push(k);
return a;
}, arr)
}, []);
console.log(res);
Merge to a single object by spreading into Object.assign(), and then get the keys:
var obj = [{"a":1},{"b":2},{"c":3}];
const result = Object.keys(Object.assign({}, ...obj));
console.log(result);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Or use Array.flatMap() with Object.keys():
var obj = [{"a":1},{"b":2},{"c":3}];
const result = obj.flatMap(Object.keys);
console.log(result);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
I'd .map and extract the first item in the Object.keys of the object being iterated over:
var obj = [{
"a": 1
}, {
"b": 2
}, {
"c": 3
}];
const result = obj.map(inner => Object.keys(inner)[0]);
console.log(result);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
If each object will definitely have only one enumerable property, then you can use Object.keys(someArray[i])[0] to get that property's name in your loop. Object.keys returns an array of the object's own, enumerable property names, and [0] gets the first entry from it. (And of course, someArray[i][theName] will give you the value of that property.)
Example:
var objX = {
x: "ecks"
};
var objY = {
y: "why"
};
var objZ = {
z: "zee"
};
var someArray = [];
someArray.push(objX, objY, objZ);
for (var i = 0; i < someArray.length; i++) {
var arrayEntry = someArray[i];
var name = Object.keys(arrayEntry)[0];
console.log(name + " is " + arrayEntry[name]);
}
Use the objects in the array as real objects.
var objX = {key: 'one', value: 'oneValue'};
var objY = {key: 'two', value: 'twoValue'};
var objZ = {key: 'three', value: 'threeValue'};
var someArray = [];
someArray.push(objX, objY, objZ); //each of these objects pushed in have 1 key/value pair
for (var i = 0; i < someArray.length; i++) {
var obj = someArray[i];
var key = obj.key;
var value = obj.value;
}
Here is a version where you could have many sub key for each of your 3 object.
var arr = [
{
"a": {
"property1": "false"
}
},
{
"b": {
"property1": "false",
"property2": "truthy"
}
},
{
"c": {
"propertycheck": "required"
}
}
]
var arr = arr.filter(function(elem){ //Filter on each object in the array
var checkKey = true; //checkKey will be the return value of filter
Object.keys(elem).forEach(function(key){ //For each key of elem
if("property1" in elem[key]){ //If value of key (object) own "property1"
checkKey = false //You want to remove the object from table so return false to filter
}
})
return checkKey
})
–
Reference to Array.filter https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/filter
This can do it:
d = [
{
"a": {
"property1": "false"
}
},
{
"b": {
"property1": "false",
"property2": "truthy"
}
},
{
"c": {
"propertycheck": "required"
}
}
]
d.filter(function(x){
if(!x[Object.keys(x)[0]].hasOwnProperty("property1"))
{return x}
})
Explanation:
d.filterwill iterate through the array of object while applying the filtering functionx[Object.keys(x)[0]]this is used to get the key a, b or c dynamically..hasOwnProperty("property1")will check if the keyproperty1is in the the object.
I'm trying to return the value of a specific key in a array of objects.
I want to return the value of "okay", so it should just be returning "15"
How can I just return this value? The code I have in place returns an array including "undefined".
const firstState = [{ bye: 14 }, { okay: 15 }, { hi: 12 }]
const test = firstState.map((item) => {
return item.okay
})
NOTE- currently returning: [undefined,15,undefined]You can take Array.map(). This method returns an array with the elements from the callback returned. It expect that all elements return something. If not set, undefined will be returned.
var students = [{
name: 'Nick',
achievements: 158,
points: 14730
}, {
name: 'Jordan',
achievements: '175',
points: '16375'
}, {
name: 'Ramon',
achievements: '55',
points: '2025'
}];
var nameArray = students.map(function (el) { return el.name; });
document.getElementById('out').innerHTML = JSON.stringify(nameArray, null, 4);
<pre id="out"></pre>
Using forEach:
var a = [];
students.forEach(function(obj){
a.push(obj.name);
})
console.log(a);
Output:
["Nick", "Jordan", "Ramon"]