How to get data from an object - javascript

How to get data from an object? I need to get data from dataValues ​​and write it down
const user = User.findOne({
}).then(e=>console.log(e))
User {
dataValues: {
id: 1,
firstName: 'Мен',
lastName: 'Bezrukov',
login: 'qqq',
password: '1234',
role: 'admin',
ip: '12345',
descipt: 'developer',
o_sebe: 'top man',
soc_set: 'vk',
age: '17',
likes__foto: '5',
coment__foto: null,
createdAt: 2023-01-15T09:06:39.000Z,
updatedAt: 2023-01-15T09:07:00.000Z
},
_previousDataValues: {
id: 1,
firstName: 'Мен',
lastName: 'Bezrukov',
login: 'qqq',
password: '1234',
role: 'admin',
ip: '12345',
descipt: 'developer',
o_sebe: 'top man',
soc_set: 'vk',
age: '17',
likes__foto: '5',
coment__foto: null,
createdAt: 2023-01-15T09:06:39.000Z,
updatedAt: 2023-01-15T09:07:00.000Z
},
uniqno: 1,
_changed: Set(0) {},
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [
'id', 'firstName',
'lastName', 'login',
'password', 'role',
'ip', 'descipt',
'o_sebe', 'soc_set',
'age', 'likes__foto',
'coment__foto', 'createdAt',
'updatedAt'
]
},
}

You can access directly the data by user.id, user.firstName or destructuring:
const {id,firstName} = user
another options is putting raw: true.
User.findOne({ where: {firstname: "some name"},
raw: true
})
This gives you directly an object with only the data instead of an sequelize instance.

Related

Remap an array to have an object above each entry in javascript

I have a prisma query that returns users on a pivot table CommunityMember. The pivot table associates to the table User. As a result, my queries into the User table do not place the object user on top of the replies. I have a lot of functions designed to run with the user object on top so I am trying to figure out how to map user into my returns so the functions can run correctly. I've tried a lot of combinations of map and have had no luck. Do you all have any ideas?
Here is my prisma query:
members = await prisma.user.findMany({
select: {
id: true,
username: true,
name: true,
bio: true,
avatar: true,
},
skip: 10 * Number(page),
take: 10,
});
It gives a result like
members:
[{id: 2, username: 'man}, {id: 3, username: 'dan'}]
I want it to look like: members:
members:
[{user: {id: 2, username: 'man'}}, {user: {id: 3. username: 'dan'}}]
If I run members[0].user I should get the data inside. It seems like a simple map function, but I have not been able to get it to work.
Another example of what I get, but do not want.
members: [
{
id: 2,
username: 'man',
name: 'A Man',
bio: 'A man.',
avatar: [Object]
},
{
id: 3,
username: 'dude',
name: 'Dude',
bio: "Dude is a #1 developer.",
avatar: [Object]
},
This is what I want.
members: [
{
//Notice the user object on top of each entry
user: {
id: 2,
username: 'man',
name: 'A Man',
bio: 'Man.',
avatar: [Object]
}
},
{
user: {
id: 3,
username: 'dude',
name: 'Dude',
bio: "Dudes a #1 developer.",
avatar: [Object]
}
},
]
Not sure I got the whole picture but wouldn't this do?
const members = [
{
id: 2,
username: 'man',
name: 'A Man',
bio: 'A man.',
avatar: {}
},
{
id: 3,
username: 'dude',
name: 'Dude',
bio: "Dude is a #1 developer.",
avatar: {}
}
].map(member => ({user: member}))
if your object looks like:
obj = {members: [{...}, ..]}
then an Array.map will solve the problem:
const userMembers = obj.members.map((member) => ({user: member});
obj.members = userMembers;

Map array and group by roles in the array object

I have an object array, And in that I have roles as a key, And values init.
Now what I want is I want to group them as per the roles.
This is my data input
{
users: [
{
firstName: 'Will',
lastName: 'Jacob',
email: 'sample#sample.com',
_id: '5e324187b5fdf167a91dfdbb',
roles: [ 'ward', 'hospital', 'hr' ],
},
{
firstName: 'Theatre',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf2f0c631a8788be59fc4',
roles: [ 'ward' ],
},
{
firstName: 'Cinema',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf62cc631a8788be59fc5',
roles: ['hospital', 'hr' ],
},
{
firstName: 'Cinema2',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf62cc631a8788be59fc5',
roles: ['ward', 'hr' ],
},
{
firstName: 'Cinema3',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf62cc631a8788be59fc5',
roles: ['hospital', 'hr' ],
},
{
firstName: 'Cinema4',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf62cc631a8788be59fc5',
roles: [ 'ward', 'hospital', 'hr' ],
}
]}
AND I want to group it by roles in them and want output something like below
{
ward: [{
firstName: 'Will',
lastName: 'Jacob',
email: 'sample#sample.com',
_id: '5e324187b5fdf167a91dfdbb',
roles: [ 'ward', 'hospital', 'hr' ],
},
{
firstName: 'Theatre',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf2f0c631a8788be59fc4',
roles: [ 'ward' ],
},
{
firstName: 'Cinema2',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf62cc631a8788be59fc5',
roles: ['ward', 'hr' ],
},
{
firstName: 'Cinema3',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf62cc631a8788be59fc5',
roles: [ 'ward', 'hospital', 'hr' ],
}],
hospital: [
{
firstName: 'Will',
lastName: 'Jacob',
email: 'sample#sample.com',
_id: '5e324187b5fdf167a91dfdbb',
roles: [ 'ward', 'hospital', 'hr' ],
},
{
firstName: 'Cinema',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf62cc631a8788be59fc5',
roles: ['hospital', 'hr' ],
},
{
firstName: 'Cinema3',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf62cc631a8788be59fc5',
roles: ['hospital', 'hr' ],
},
{
firstName: 'Cinema4',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf62cc631a8788be59fc5',
roles: [ 'ward', 'hospital', 'hr' ],
}],
hr: [{
firstName: 'Will',
lastName: 'Jacob',
email: 'sample#sample.com',
_id: '5e324187b5fdf167a91dfdbb',
roles: [ 'ward', 'hospital', 'hr' ],
},
{
firstName: 'Cinema',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf62cc631a8788be59fc5',
roles: ['hospital', 'hr' ],
},
{
firstName: 'Cinema2',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf62cc631a8788be59fc5',
roles: ['ward', 'hr' ],
},
{
firstName: 'Cinema3',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf62cc631a8788be59fc5',
roles: ['hospital', 'hr' ],
},
{
firstName: 'Cinema4',
lastName: 'Manager',
email: 'sample#sample.com',
_id: '5e3cf62cc631a8788be59fc5',
roles: [ 'ward', 'hospital', 'hr' ],
}
]
}
How can I achieve this? I am looking for a function to process this and return the output as requried.
Please find the Array.reduce implementation.
Logic
Loop through each object in array
Loop through the roles array in each object.
Check if the accumulator have a node with that role, if yes push to that node in accumulator, or else create a node with that name and current object as an element of that node
Working Example
const data = {"users":[{"firstName":"Will","lastName":"Jacob","email":"sample#sample.com","_id":"5e324187b5fdf167a91dfdbb","roles":["ward","hospital","hr"]},{"firstName":"Theatre","lastName":"Manager","email":"sample#sample.com","_id":"5e3cf2f0c631a8788be59fc4","roles":["ward"]},{"firstName":"Cinema","lastName":"Manager","email":"sample#sample.com","_id":"5e3cf62cc631a8788be59fc5","roles":["hospital","hr"]},{"firstName":"Cinema2","lastName":"Manager","email":"sample#sample.com","_id":"5e3cf62cc631a8788be59fc5","roles":["ward","hr"]},{"firstName":"Cinema3","lastName":"Manager","email":"sample#sample.com","_id":"5e3cf62cc631a8788be59fc5","roles":["hospital","hr"]},{"firstName":"Cinema4","lastName":"Manager","email":"sample#sample.com","_id":"5e3cf62cc631a8788be59fc5","roles":["ward","hospital","hr"]}]};
const output = data.users.reduce((acc, curr) => {
curr.roles.forEach((role) => {
acc[role] ? acc[role].push(curr) : acc[role] = [curr];
})
return acc;
}, {});
console.log(output);
You can use array#reduce to group your data based on roles. You can iterate through each object and create separate array for each role type. This solution only require single traversal of your input array.
const input = { users: [ { firstName: 'Will', lastName: 'Jacob', email: 'sample#sample.com', _id: '5e324187b5fdf167a91dfdbb', roles: [ 'ward', 'hospital', 'hr' ], }, { firstName: 'Theatre', lastName: 'Manager', email: 'sample#sample.com', _id: '5e3cf2f0c631a8788be59fc4', roles: [ 'ward' ], }, { firstName: 'Cinema', lastName: 'Manager', email: 'sample#sample.com', _id: '5e3cf62cc631a8788be59fc5', roles: ['hospital', 'hr' ], }, { firstName: 'Cinema2', lastName: 'Manager', email: 'sample#sample.com', _id: '5e3cf62cc631a8788be59fc5', roles: ['ward', 'hr' ], }, { firstName: 'Cinema3', lastName: 'Manager', email: 'sample#sample.com', _id: '5e3cf62cc631a8788be59fc5', roles: ['hospital', 'hr' ], }, { firstName: 'Cinema4', lastName: 'Manager', email: 'sample#sample.com', _id: '5e3cf62cc631a8788be59fc5', roles: [ 'ward', 'hospital', 'hr' ], }]},
result = input.users.reduce((r, o) => {
o.roles.forEach(role => {
r[role] ??= [];
r[role].push(JSON.parse(JSON.stringify(o)));
});
return r;
},{});
console.log(result);
You can just go over each input element and then over each role in the input element and add the element to the corresponding array in a by-role object (creating the array as needed):
const byRole = {}
for (const obj of input.users) {
for (const role of obj.roles) {
if (!byRole[role]) byRole[role] = []
byRole[role].push(obj)
}
}
console.log(byRole)
There would be also a less imperative, more functional approach, if you prefer that:
const byRole = Object.fromEntries(
[...new Set(input.users.map(obj => obj.roles).flat())]
.map(role => [role, inputArray.filter(obj => obj.roles.includes(role))])
)
console.log(byRole)
Since you seem to be working with MongoDB records, another possibility would be to group it already in your database query, offloading that work to the database server:
const queryResult = await db.users.aggregate([
// Optionally add a $match step here to filter: { $match: { ... } }
{ $addFields: { role: '$roles' } },
{ $unwind: '$role' },
{ $group: { _id: '$role', records: { $push: '$$ROOT' } } }
])
const byRole = Object.fromEntries(
queryResult.map(({ _id, records }) => [_id, records])
)
(This will leave an extra field role in each record though, but I guess you won't mind.)

how to refactor array from array to single array using javascript

const arr = [
[
{
_id: "6136096f4255d84bcb4a7144",
user_id: "5fbfa729fc46a415ce5503a6",
picture: [Array],
timestamp: 1630931311227,
},
user: {
_id: "5fbfa729fc46a415ce5503a6",
first_name: "ABC",
last_name: "AAA",
picture: [Object]
}
],
[
{
_id: "613609414255d84bcb4a7122",
user_id: "5fbf6f91aff7f3320a906547",
picture: [Array],
device_platform: 'ios',
timestamp: 1630931265409
},
user: {
_id: "5fbf6f91aff7f3320a906547",
first_name: 'EEE',
last_name: 'TTT',
picture: [Object]
}
],
[
{
_id: "613709f49223350dfdaec618",
user_id: "5fbfa748fc46a415ce5503a8",
picture: [Array],
timestamp: 1630996980379
},
{
_id: "613609184255d84bcb4a710a",
user_id: "5fbfa748fc46a415ce5503a8",
picture: [Array],
timestamp: 1630931224383,
},
user: {
_id: "5fbfa748fc46a415ce5503a8",
first_name: 'GRT',
last_name: 'GGG',
picture: [Object]
}
]
]
I merged push into the object one were like res = [{_id: ..., user_id: ...},{_id: ..., user_id: ...}] and other one was like user = {_id: 5fbfa748fc46a415ce5503a8, first_name: 'AAA',last_name : 'DD'}
res.user = user;
array.push(res)
I merged two object it bring up this but it does not seems to be correct response.
when I used var merged = [].concat.apply([], arr); it does not work
how to fix with correct format.
Working code! just remove the user key from the array, and one note ) use const/let instead of var
const arr = [
[
{
_id: "6136096f4255d84bcb4a7144",
user_id: "5fbfa729fc46a415ce5503a6",
picture: [Array],
timestamp: 1630931311227,
},
{
_id: "5fbfa729fc46a415ce5503a6",
first_name: "ABC",
last_name: "AAA",
picture: [Object]
}
],
[
{
_id: "613609414255d84bcb4a7122",
user_id: "5fbf6f91aff7f3320a906547",
picture: [Array],
device_platform: 'ios',
timestamp: 1630931265409
},
{
_id: "5fbf6f91aff7f3320a906547",
first_name: 'EEE',
last_name: 'TTT',
picture: [Object]
}
],
[
{
_id: "613709f49223350dfdaec618",
user_id: "5fbfa748fc46a415ce5503a8",
picture: [Array],
timestamp: 1630996980379
},
{
_id: "613609184255d84bcb4a710a",
user_id: "5fbfa748fc46a415ce5503a8",
picture: [Array],
timestamp: 1630931224383,
},
{
_id: "5fbfa748fc46a415ce5503a8",
first_name: 'GRT',
last_name: 'GGG',
picture: [Object]
}
]
]
const merged = [].concat.apply([], arr);
console.log(merged);

How to add a value to a JSON array of objects?

i have a json array of objects like this, to pass the values to the datatables.
[{ _id: '58a2b5941a9dfe3537aad540',
Country: 'India',
State: 'Andhra Pradesh',
District: 'Guntur',
Division: '',
Village: 'Macharla',
FarmerName: 'Vijay',
Address: '',
Pin: '',
PrimaryContact: '9160062222',
OtherContacts: '',
Email: '',
updatedAt: '2017-02-14T04:39:01.000Z',
modifiedBy: '',
createdAt: '2017-02-14T04:39:01.000Z' },
{ _id: '58a2b5941a9dfe3537aad541',
Country: 'India',
State: 'Telangana',
District: 'Karimnagar',
Division: '',
Village: 'Sirisilla',
FarmerName: 'Subhash Rao',
Address: '',
Pin: '',
PrimaryContact: '8121787878/9441967878',
OtherContacts: '',
Email: '',
updatedAt: '2017-02-14T04:39:01.000Z',
modifiedBy: '',
createdAt: '2017-02-14T04:39:01.000Z' },
{ _id: '58a2b5941a9dfe3537aad542',
Country: 'India',
State: 'Telangana',
District: 'Medak',
Division: '',
Village: 'Jagur',
FarmerName: 'Ramachandra',
Address: '',
Pin: '',
PrimaryContact: '9346481116',
OtherContacts: '',
Email: '',
updatedAt: '2017-02-14T04:39:01.000Z',
modifiedBy: '',
createdAt: '2017-02-14T04:39:01.000Z' },
{ _id: '58a2b5941a9dfe3537aad543',
Country: 'India',
State: 'Telangana',
District: 'Mahaboob Nagar',
Division: '',
Village: 'annugal',
FarmerName: 'Rajesh Reddy',
Address: '',
Pin: '',
PrimaryContact: '93464593808',
OtherContacts: '',
Email: '',
updatedAt: '2017-02-14T04:39:01.000Z',
modifiedBy: '',
createdAt: '2017-02-14T04:39:01.000Z' } ]
Now i want to add one more object like "recordsTotal":600 outside of this array. how can i achieve it?
Finally i want the data like this
{
"draw": 1,
"recordsTotal": 1,
"recordsFiltered": 1,
"data": [{
"VendorID": "V0000051",
"Name": "STAPLES CONTRACT AND COMMERCIAL INC",
"Mnemonic": "CORPORATE",
"TermsDescription": "INV 2/30 NET 31",
"ActiveYn": "Y"
}]
}
First of all store the json array in a variable like
var datalist=[{ _id: '58a2b5941a9dfe3537aad540',
Country: 'India',
State: 'Andhra Pradesh',
District: 'Guntur',
Division: '',
Village: 'Macharla',
FarmerName: 'Vijay',
Address: '',
Pin: '',
PrimaryContact: '9160062222',
OtherContacts: '',
Email: '',
updatedAt: '2017-02-14T04:39:01.000Z',
modifiedBy: '',
createdAt: '2017-02-14T04:39:01.000Z' },
{ _id: '58a2b5941a9dfe3537aad541',
Country: 'India',
State: 'Telangana',
District: 'Karimnagar',
Division: '',
Village: 'Sirisilla',
FarmerName: 'Subhash Rao',
Address: '',
Pin: '',
PrimaryContact: '8121787878/9441967878',
OtherContacts: '',
Email: '',
updatedAt: '2017-02-14T04:39:01.000Z',
modifiedBy: '',
createdAt: '2017-02-14T04:39:01.000Z' },
{ _id: '58a2b5941a9dfe3537aad542',
Country: 'India',
State: 'Telangana',
District: 'Medak',
Division: '',
Village: 'Jagur',
FarmerName: 'Ramachandra',
Address: '',
Pin: '',
PrimaryContact: '9346481116',
OtherContacts: '',
Email: '',
updatedAt: '2017-02-14T04:39:01.000Z',
modifiedBy: '',
createdAt: '2017-02-14T04:39:01.000Z' },
{ _id: '58a2b5941a9dfe3537aad543',
Country: 'India',
State: 'Telangana',
District: 'Mahaboob Nagar',
Division: '',
Village: 'annugal',
FarmerName: 'Rajesh Reddy',
Address: '',
Pin: '',
PrimaryContact: '93464593808',
OtherContacts: '',
Email: '',
updatedAt: '2017-02-14T04:39:01.000Z',
modifiedBy: '',
createdAt: '2017-02-14T04:39:01.000Z' } ]
then declare another json object and initialize values like
var FinalJsonObject={"draw": 1,
"recordsTotal": 1,
"recordsFiltered": 1,
"data": datalist}

Why are these objects not equal?

I am trying to write a test in JavaScript using Mocha and Chai. At the end of the test, I want to compare 2 JavaScript objects, these should be equal.
The output I get from the test is the following:
{ publication:
{ publication_id: 'pubFetch1',
title: 'Publication Fetcher',
person_id: 'uploader',
URL: 'http://www.pubfetchertest.com',
publication_year: '2015',
upload_date: '2015-05-05 00:00:00',
page_count: '5',
type: 'paper',
rating: '0',
votes: '0',
abstract: 'Testing the Publication Fetcher',
location: 'location' },
comments:
[ { commentID: 'comment1',
personID: 'uploader',
firstName: 'First',
lastName: 'Last',
text: 'Comment Content',
time: '2015-05-05 10:24:36',
reactions: [],
myComment: true },
{ commentID: 'comment2',
personID: 'author1',
firstName: 'First',
lastName: 'Last',
text: 'Comment Content',
time: '2015-05-05 11:01:45',
reactions: [Object],
myComment: false } ],
keywords: [ 'keyword1', 'keyword2', 'keyword3' ],
uploader: { person_id: 'uploader', first_name: 'First', last_name: 'Last' },
score: 5,
authors:
[ { person_id: 'author1', first_name: 'First', last_name: 'Last' },
{ person_id: 'author2', first_name: 'First', last_name: 'Last' },
{ person_id: 'author3', first_name: 'First', last_name: 'Last' } ],
editors: [],
publishers: [] }
While the object I want to compare it with, is the following:
{ publication:
{ publication_id: 'pubFetch1',
title: 'Publication Fetcher',
person_id: 'uploader',
url: 'http://www.pubfetchertest.com',
publication_year: '2015',
upload_date: '2015-05-05 00:00:00',
page_count: '5',
type: 'paper',
rating: '0',
votes: '0',
abstract: 'Testing the Publication Fetcher',
location: 'location'},
comments:
[{ commentID: 'comment1',
personID: 'uploader',
firstName: 'First',
lastName: 'Last',
text: 'Comment Content',
time: '2015-05-05 10:24:36',
reactions: [],
myComment: true},
{ commentID: 'comment2',
personID: 'author1',
firstName: 'First',
lastName: 'Last',
text: 'Comment Content',
time: '2015-05-05 11:01:45',
reactions: [{ commentID: 'comment3',
personID: 'author2',
firstName: 'First',
lastName: 'Last',
text: 'Comment Content',
time: '2015-05-05 11:02:10',
reactions: [],
replyID: 'comment2',
myComment: false}],
myComment: false}],
keywords: ['keyword1', 'keyword2', 'keyword3'],
uploader: {person_id: 'uploader',
first_name: 'First',
last_name: 'Last'},
score: 5,
authors: [{person_id: 'author1',
first_name: 'First',
last_name: 'Last'},
{person_id: 'author2',
first_name: 'First',
last_name: 'Last'},
{person_id: 'author3',
first_name: 'First',
last_name: 'Last'}],
editors: [],
publishers: []
};
I don't see any difference in these 2 objects, so why does JavaScript say these are different? I am using JSON.stringify(obj1) === JSON.stringify(obj2) to compare these 2 objects.
JSFiddle: http://jsfiddle.net/9akeh8bh/4/
There are differences between the two objects; some keys have underscores and some are uppercase.
Have you tried standardising the key names and seeing if the comparison works?
Your reactions are different, some contain Objects, some do not.

Categories