This question already has answers here:
How do I return the response from an Observable/http/async call in angular?
(10 answers)
Closed 5 years ago.
I have this:
[Object]
0:Object
zipCode:"1232"
Now what i want is to get zipCode from object i tried with this:
Object[0].zipCode but i get message that 0 is undefined
Update:
this.restService.getByParam("findAddress", customerId)
.subscribe(results => {
this.address = results.payload;
});
And then i get that object and now i want to do something like this:
console.log(this.address[0].zipCode
var obj = { 0 : {'zipCode':"1232"} }
for(var key in obj) {
console.log(obj[key].zipCode);
}
here u go :
var foo = { 0 : {'zipCode':"1232"} }
for(var key in foo) {
var value = foo[key];
}
console.log(value); // this give you 'zipCode':"1232"
console.log(value['zipCode']); // this give you "1232"
https://jsfiddle.net/emilvr/9d58ebuy/
Related
This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 3 months ago.
What is happening in the code on line 10 ({[last]: newObj}) in the following snippet:
How is JS able to use the value of parameter last instead of using last as the property name?
let first = 'first';
let last = 'last';
function foo(first, last) {
let newObj = {
name: 'newObj'
};
let obj = {};
Object.assign(obj, {[last]: newObj});
return obj;
}
console.log(foo('bye', 'hey')); // { hey: { name: 'newObj' } }
Thanks.
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 3 years ago.
I am trying to use a variable when declaring an object:
var name1 = "object1";
var data1 = 3;
create_object(name1, data1);
function create_object(name, data) {
var x = {
name: data
}
return x
}
I want x to be stored as
var x = {
object1: 3
}
But my function will make
var x = {
name: 3
}
Is there a way to pass a variable when declaring the name of a child inside an object?
Thanks a lot
To specify a name of a property from a variable you need to use the square brackets notation like this:
function create_object(name, data) {
var x = {};
x[name] = data;
return x;
}
This question already has answers here:
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Closed 6 years ago.
I m trying to access an object property through a string.
What I m trying to do is :
const data = {
key: {
subKey: 'value'
}
};
const theString = 'key.subKey';
function accessPropFromString(obj, stringCall) {
// I don't know what to put on here
}
console.log(accessPropFromString(data, theString)) // prints 'value'
I absolutely don't know how to make that happen...
How can I do this kind of stuff with JavaScript ?
You could split the path and reduce the object.
const getValue = (object, path) => path.split('.').reduce((o, k) => (o || {})[k], object),
data = { key: { subKey: 'value' } },
theString = 'key.subKey';
console.log(getValue(data, theString));
This should work. Assuming, you have fixed format.
function accessPropFromString(obj, stringCall) {
var splitter =stringCall.split(".");
return data[splitter[0]][splitter[1]];
}
Demo
See comments inline:
const data = {
key: {
subKey: 'value'
}
};
const theString = 'key.subKey';
function accessPropFromString(obj, stringCall) {
// Split the string into individual parts
var parts = stringCall.split(".");
// You can use strings to lookup properties on objects
// if you use bracket notation, like this;
// parts["key"]["subKey"]
// So, parts[0] will be "key" and parts[1] will be "subKey"
return obj[parts[0]][parts[1]];
}
console.log(accessPropFromString(data, theString)) // prints 'value'
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 6 years ago.
I want to create a json object in node.js that looks like this;
{ WantThisName: { property_length: 8 } }
Here is my code;
var property_name = "WantThisName"
var length = 8;
var Obj_bin;
Obj_bin= {property_name: {property_length:length} };
console.log (Obj_bin);
The console output is;
{ property_name: { property_length: 8 } }
The problem lies with property_name not getting the contents of the variable property_name.
You can use computed (dynamic) property names:
> foo = "foozz"
'foozz'
> {[foo]: 42}
{ foozz: 42 }
They're part of the Enhanced Object Literals of es2015 (https://github.com/lukehoban/es6features#enhanced-object-literals).
For your example:
> var property_name = "WantThisName";
var property_name = "WantThisName";
undefined
> var length = 8;
undefined
> {[property_name]: { property_length: length}}
{ WantThisName: { property_length: 8 } }
>
try
Obj_bin = {};
Obj_bin[property_name] = { property_length: 8 };
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
I am trying to get the first value for an object like:
{
"_id":"123",
"list":{"56":{"name":"Great ","amount":100,"place":"Town"},"57":{"name":"Great 2","amount":200,"place":"City"}},
"pop":[2,3]
}
This is 1 object of around 100 or so. I am trying to get the amount from the first value of list property in each of the 100 objects ? i.e. so above it would be 100
How would I do that ? I am using underscore JS ?
You are defining list as object . Object properties are in order of they defined, but we cannot trust its order . I think there is Chrome bug about it https://code.google.com/p/v8/issues/detail?id=164
the following first method gives first element from list object , this function will works most cases , if object is empty it will return undefined value.
var data = {
"_id":"123",
"list":{
"56":{"name":"Great ","amount":100,"place":"Town"},
"57":{"name":"Great 2","amount":200,"place":"City"}
},
"pop":[2,3]
};
function first( data ){
for ( var i in data ){
if ( data.hasOwnProperty(i) ) break;
}
return data.hasOwnProperty(i) ? data[i] : undefined;
}
first( data.list ).amount;
If you want to keep order of list .. define them as Array . example
var data = {
"_id":"123",
"list":[
{"id" : "56" ,"name":"Great ","amount":100,"place":"Town"},
{"id" : "57" ,"name":"Great 2","amount":200,"place":"City"}
],
"pop":[2,3]
};
and access them as data.list[0]
You can try this
function getFirstAmount(obj){
for(var key in obj){
return obj[key].amount;
}
}
If you need to get all keys, you can try this
function getAmounts(obj){
var amounts = [];
var i = 0;
for(var key in obj){
amounts[i] = obj[key].amount;
i++;
}
}
return amounts;
}
//call function
var firstAmount = getFirstAmount(obj.list);