This question already has answers here:
How to list the properties of a JavaScript object?
(18 answers)
Closed 4 years ago.
This is my spinet:
$scope.foodtypes = {
'GRAINS': {
'HOT': ['Rice','Beans','Sogum'],
'ROOT': ['Yam','Potato','Cassava']
}
}
I like to store the exact word "GRAINS" or "ROOT" in a variable for decision making:
something like:
if foodtypes is "GRAINS"
then ......
if foottypes is "ROOT"
then ......
Please how do I capture "GRAIN" or "ROOT" for use?
Assuming that foodtypes is a JSON string that represents an object that has all food types as first-level key, you could do something like this:
Object.keys(JSON.parse($scope.foodtypes));
This will return an array of strings ["GRAINS", "ROOT"].
Related
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 3 years ago.
Let's say I have an object
const myArray = {
a : "hello"
}
and I have a string with the same name of that object
like
var type ="myArray";
when I do console.log(type);
output: myArray
but I want to out that object to the console which has the same name as the value of variable type.
How should I do that?
Thanks in advance
If it's a global variable, it will be stored in window.
So, you can do something like console.log(window[type]) to access to value.
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I have an array that looks like this:
cardNumber: "2344234454562036"
cardType: "Visa"
cardholderName: "Yeyy"
cvv: "123"
expiryMonth: 12
expiryYear: 2022
postalCode: "52636"
redactedCardNumber: "••••••••••••2036"
__proto__: —'
I need to get the value of redactedCardNumber from that array.
The first thing that I am confused about is that when i print the above 'array' in the console, it actually show an object before everything! so is this an array or an object?
also, what is the best way of getting the specific item like 'redactedCardNumber' from it?
It looks like an object. If so, you can access it by obj.redactedCardNumber.
This question already has answers here:
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Closed 4 years ago.
Sorry if the title is confusing, I'm confused my self.
So its like this, I get a string from api like this: 'data.data.type'.
And I need to turn it into this response['data]['data]['type'].
Any idea how I can achieve this?
Assuming response is a single object , you can get the value of type like this
var response = {
data: {
data: {
type: "Here is type"
}
}
}
console.log(response['data']['data']['type'])
This question already has answers here:
Which characters are valid/invalid in a JSON key name?
(4 answers)
Closed 5 years ago.
I have to make an object with - like:
let myObject = {
one-property: 'assignee'
}
but JavaScript does not allow this. So any trick to make this work with -? My whole backend has objects with - on keys.
You have to put the key in quotes
let myObject = {
'one-property': 'assignee'
}
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
how to get data from object with define string?
case:
var data = [
{name:"Sharma",country:"India"},
{name:"Udin",country:"Indonesia"},
{name:"John Carter",country:"Mars"}
];
getData(data,"country");
function getData(data,element){
console.log(data[1].element);
}
i want to get country but result is undefinded, how to fix this?
You would need to know both the index and the property
function getData(data,index,element){
console.log(data[index][element]);
}
getData(data,1,"country");
function getData(data,element){
console.log(data[1][element]);
}
That's the correct way to access the value by using a key that is a string.