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);
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:
Why I get Undefined email data result from findAll Sequelize?
(2 answers)
Closed 2 years ago.
Why am I getting an error undefined?
user.findAll({
where: { email: req.body.email },
raw: true
})
.then(function (resUser) {
console.log(resUser.name)
})
I'm trying to show the name:
[
{
id: 1,
name: 'user',
email: 'user#outlook.com',
createdAt: 2020-08-27T22:52:39.059Z,
updatedAt: 2020-09-01T21:33:27.458Z,
}
]
Thanks
You use findAll that returns an array of found objects so either you should get resUser[0] OR if you're sure you always get one record only then you can use findOne and you'll get an object or null if no record was found.
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
sorry before if this is an duplicate or have similarity with other post, but I cannot find problem with the same context as I have.
So I have an array of object, something like this
const queryResult = [
{ token: 'c5WKMXW8QdCFUg4q8ica' },
{ token: 'Othertoken' },
{ token: 'moreothertokens'},
]
So, i want to merge those 3 tokens into an array, something like this
['c5WKMXW8QdCFUg4q8ica', 'Othertoken', 'moreothertokens']
I am using forEach for the solution at the moment. But is there any shortcut or cleaner code?
Thank you.
Try this:
const tokens = queryResult.map(x => x.token);
If you have only one property in the objects, you could take a flat map approach with Object.values as callback.
const
queryResult = [{ token: 'c5WKMXW8QdCFUg4q8ica' }, { token: 'Othertoken' }, { token: 'moreothertokens'}],
tokens = queryResult.flatMap(Object.values);
console.log(tokens);
You could use map with object destructing
const queryResult = [
{ token: "c5WKMXW8QdCFUg4q8ica" },
{ token: "Othertoken" },
{ token: "moreothertokens" },
]
const tokens = queryResult.map(({ token }) => token)
console.log(tokens)
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:
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