This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
Been looking around haven't find a solution.. Might be a really stupid question (probably) but haven't found a way to access to it.
I have the class Iphone:
export class Iphone{
version: string;
fixes = [
{fixlcdprice:null},
{fixspeakerprice:null}
];
}
then I have an array of Iphone with data
export const IPHONES:Iphone[]=[
{
version:'Iphone 4',
fixes:[
{fixlcdprice:19},
{fixspeakerprice:19}
]
},
{
version:'Iphone 4s',
fixes: [
{fixlcdprice:19},
{fixspeakerprice:29}
]
}
]
trying to access the price of the fixes but I can't.
have tryed
Iphone.fixes[0] <-- returns (object, object)
then tryed
Iphone.fixes[0[0]] <-- returns nothing..
Iphone.fixes.fixlcdprice <-- doesnt work
Looks like you want
Iphone.fixes[0].fixlcdprice
Iphone=[
{
version:'Iphone 4',
fixes:[
{fixlcdprice:19},
{fixspeakerprice:19}
]
},
{
version:'Iphone 4s',
fixes: [
{fixlcdprice:19},
{fixspeakerprice:29}
]
}
]
Iphone[0].fixes[0] gives you the disired result
Related
This question already has answers here:
How can I get the full object in Node.js's console.log(), rather than '[Object]'?
(19 answers)
Closed 1 year ago.
First of all, I'm new in node js. I'm trying to create a REST API which looks like this:
[{
id: 1,
name: 'tushar hasan',
email: 'mtushar**#gmail.com',
devices: {
id: 2,
device_mac: '4A:34:ER:34:12',
relays: [
{
relay_name:"r1",
status:0
},
{
relay_name:"r2",
status:1
}
]
}
}]
But no matter what I'm trying it keeps on showing me below output:
[
{
id: 1,
name: 'tushar hasan',
email: 'mtushar**#gmail.com',
devices: {
id: 2,
device_mac: '4A:34:ER:34:12',
relays: [Array]
}
}
]
I'm providing you source code that's in below:
var x = [];
var data = {id :1, name:'tushar hasan', email:'mtushar**#gmail.com'};
var relay1 = {relay_name:'r1', status:0};
var relay2 = {relay_name:'r2', status:1};
var devices = {id:2, device_mac: '4A:34:ER:34:12'};
devices.relays = [];
devices.relays.push(relay1);
devices.relays.push(relay2);
data.devices = devices;
x.push(data);
console.log(JSON.stringify(x));
console.log(x);
By the way, when I call JSON.stringify(x), it provides me elements inside the relay array property. But without JSON.stringify(x) it doesn't show the elements inside relay property.
Thanks in advance.
Your code is fine. By default, when converting an object to string (like console.log does), the nested arrays will get converted to the text [Array]). The fact that you see the correct output when you call JSON.stringify asserts that it's really there.
This question already has answers here:
How to determine if Javascript array contains an object with an attribute that equals a given value?
(27 answers)
Closed 3 years ago.
Right now I have an array that looks like this
const array = [
{
value: 'received',
title: 'Hjá Birgja',
},
{
value: 'pending',
title: 'Yfirstandandi',
},
{
value: 'processing',
title: 'Í vinnslu',
},
]
and I would like this to return true
if(array.includes('processing'){
// do something
}
not exactly what you wanted since i'm not sure if you only wanted to search the value key but here's a solution
if (array.find(i => i.value === 'processing')) {
// do something
}
This question already has answers here:
Can't access object property of a Mongoose response
(4 answers)
Closed 4 years ago.
I am encountering a weird issue.
I have searched and found a document in my mongoDB using mongoose by using model.findOne() like so:
Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
res.send(existingDoc );
});
Now, everything works until now, it sends the json I expected to get. The looks like so:
{
"_id": "5bf388cf170a974770c5c942",
"ID": "11/2018",
"date": "2018-11-20T04:08:47.997Z",
"total": {
"total_market_cap": [
64301.06256298704
]
}
}
The problem is that when I try to access these values for example:
console.log(existingDoc.total);
I get undefined. Tried also using:
console.log(existingDoc['total']);
And I still get undefined.
It returned undefined for everything except the _id and __v. like it is an empty object, although it is not.
Can you try to convert it toObject
Model.findOne({
ID: ID
}).then(existingDoc => {
console.log(existingDoc );
let newdoc = existingDoc.toObject();
console.log(newdoc.myProperty)
res.send(existingDoc );
});
This question already has answers here:
Destructuring deep properties
(4 answers)
Closed 5 years ago.
Say I have an object with a shape like so:
{
rows: [
{some: fields,
go: here}]
}
, and say that, in a particular case, I knew that the length of rows is 1. How could I extract {some: fields, go: here} through destructuring?
I have attempted: {rows: [stuff]}, and {rows: stuff} but in both cases console.log(stuff) prints [{some: fields, go: here}] How can I do this through destructuring?
{rows: [stuff]} works fine:
const obj = {
rows: [
{some: 'fields',
go: 'here'}]
};
const { rows: [stuff] } = obj;
console.log(stuff);
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
Tell me what I am missing here. I have the follow javascript object.
[ { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD',
handle: '123',
userId: 'ABC123'} ]
When I do the following
success: function (registration) {
console.log(registration);
console.log(registration.handle);
Console log writes out the object as defined above. However when I do registration.handle I get an error saying "undefined." If registration is the above object why does registration.handle not work?
what am I missing?
You have an array containing an object. The properties you are trying to access are members of the object, not the array.
You must first get a reference to the object before you access its properties.
registration[0].handle
Try this
var registration=[ { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD', handle: '123', userId: 'ABC123'} ]
alert(registration[0].handle)
DEMO
You are accessing the member of an object.
Do it like this way
success: function(registration) {
$.each(registration, function(index, data) {
var handle = data.handle;
console.log('id is getting now ' + handle);
});
}
Yes you first need to access array element then you can find object
console.log(registration[0].handle);
it is because you are having array so to access it try
registration[0].handle
EXAMPLE
CASE 1
registration = [ { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD', handle: '123', userId: 'ABC123'} ];
console.log(registration[0].handle);
CASE 2
registration = { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD', handle: '123', userId: 'ABC123'};
console.log(registration.handle);