I'm having a problem into convert json into a string array
I need transform a json into a string array because I will do a INSERT into our database dynamically, because this need work for any json and I don't know how is the JSON that I will receive.
This is my response
[
{
consumer: 'consumer a',
orders: [{
orderNumber: 1,
details: {
dates: { deadline: '2022-02-17' },
status: [{ id: 1, status: 'Pending', date: '2022-01-01' },
{ id: 2, status: 'Awaiting Payment', date: '2022-01-02' },
{ id: 3, status: 'Awaiting Fulfillment', date: '2022-01-02' }]
}
},
{
orderNumber: 2,
details: {
dates: { deadline: '2022-03-17' },
status: [{ id: 1, status: 'Pending', date: '2022-01-01' }]
}
}]
},
{
consumer: 'consumer b',
orders: [{
orderNumber: 6,
details: {
dates: { deadline: '2022-02-17' },
status: [{ id: 1, status: 'Pending', date: '2022-01-01' }]
}
},
{
orderNumber: 7,
details: {
dates: { deadline: '2022-03-17' },
status: [{ id: 1, status: 'Pending', date: '2022-03-01' },
{ id: 2, status: 'Awaiting Payment', date: '2022-04-02' },
{ id: 3, status: 'Awaiting Fulfillment', date: '2022-05-02' }]
}
}]
}
]
I need this result
['consumer a', '1', '1'],
['consumer a', '1', '2'],
['consumer a', '1', '3'],
['consumer a', '2', '1'],
['consumer b', '6', '1'],
['consumer b', '7', '1'],
['consumer b', '7', '2'],
['consumer b', '7', '3']
I created a map for reach this objective
like this
['consumer', 'consumer.orders.orderNumber', 'consumer.orders.detaisl.status.id']
But can't do that, someone have any idea?
You can use a reduce function then traverse each order and again for each status, then return the accumulator
const arr = [
{
consumer: 'consumer a',
orders: [{
orderNumber: 1,
details: {
dates: { deadline: '2022-02-17' },
status: [{ id: 1, status: 'Pending', date: '2022-01-01' },
{ id: 2, status: 'Awaiting Payment', date: '2022-01-02' },
{ id: 3, status: 'Awaiting Fulfillment', date: '2022-01-02' }]
}
},
{
orderNumber: 2,
details: {
dates: { deadline: '2022-03-17' },
status: [{ id: 1, status: 'Pending', date: '2022-01-01' }]
}
}]
},
{
consumer: 'consumer b',
orders: [{
orderNumber: 6,
details: {
dates: { deadline: '2022-02-17' },
status: [{ id: 1, status: 'Pending', date: '2022-01-01' }]
}
},
{
orderNumber: 7,
details: {
dates: { deadline: '2022-03-17' },
status: [{ id: 1, status: 'Pending', date: '2022-03-01' },
{ id: 2, status: 'Awaiting Payment', date: '2022-04-02' },
{ id: 3, status: 'Awaiting Fulfillment', date: '2022-05-02' }]
}
}]
}
]
const result = arr.reduce((acc,val)=>{
val.orders.forEach(o=>o.details.status.forEach(s=>acc.push([val.consumer, o.orderNumber.toString(), s.id.toString()])))
return acc
},[])
console.log(result)
Related
I have to use the data I receive, in particular I have to be able to use the data in 'properties':
I don't understand how to call them, instead of console.log
I tried to use an Object.values(), but so I can't access the 'properties' data that I have to reuse later.
const response = await notion.databases.query({
database_id: databaseId,
page_size: 2
});
var effort_data = []
effort_data.push({
response
})
effort_data.map(data => {
Object.values(data.response.results).map((data) => {
console.log('data', data)
})
})
this is my response :
data {
object: 'page',
id: '3b8e203e-79ae-4ddc-ba3a-e0f5b30f4d4e',
created_time: '2023-01-25T13:51:00.000Z',
last_edited_time: '2023-01-25T13:59:00.000Z',
created_by: { object: 'user', id: '204c1b2e-0c59-4605-be45-de1fc9916416' },
last_edited_by: { object: 'user', id: '204c1b2e-0c59-4605-be45-de1fc9916416' },
cover: null,
icon: { type: 'emoji', emoji: '👽' },
parent: {
type: 'database_id',
database_id: '1b02ff59-8cdb-47fb-a6f9-5904bcecbe72'
},
archived: false,
properties: {
Assignees: { id: '%24v1Q', type: 'people', people: [] },
Type: { id: '9dB%5E', type: 'select', select: [Object] },
Progetti: {
id: '%3AirU',
type: 'relation',
relation: [Array],
has_more: false
},
Epics: { id: 'B%7BSE', type: 'relation', relation: [], has_more: false },
'Creato da': { id: 'D%5Dzr', type: 'created_by', created_by: [Object] },
'Flussi coinvolti': { id: 'EFdR', type: 'relation', relation: [], has_more: false },
'Sprint corrente?': { id: 'G%5Cvg', type: 'formula', formula: [Object] },
Sprint: { id: 'Jz.%40', type: 'multi_select', multi_select: [] },
'🏃 Sprint': { id: 'O%3DHc', type: 'relation', relation: [], has_more: false },
Settore: { id: 'XYh%5D', type: 'select', select: null },
Timeline: { id: '_G%2Bl', type: 'date', date: [Object] },
Tasks: {
id: 'bXv%3F',
type: 'relation',
relation: [Array],
has_more: false
},
'Data creazione': {
id: 'e_r%7D',
type: 'created_time',
created_time: '2023-01-25T13:51:00.000Z'
},
'Epic status': { id: 'nRGT', type: 'rollup', rollup: [Object] },
'Ultimo aggiornamento da': { id: 'ogbh', type: 'last_edited_by', last_edited_by: [Object] },
'Data ultimo aggiornamento': {
id: 'p%5CYK',
type: 'last_edited_time',
last_edited_time: '2023-01-25T13:59:00.000Z'
},
Priority: { id: '%7BMEq', type: 'select', select: [Object] },
Status: { id: '%7CF4-', type: 'select', select: [Object] },
Name: { id: 'title', type: 'title', title: [Array] }
},
url: 'https://www.notion.so/Endpoint-per-ordini-card-da-esterni-3b8e203e79ae4ddcba3ae0f5b30f4d4e'
}
I have 2 tables booking and parking. In the booking table there is parkingId which refers to parking model. In the parking model there is a field named availability which is following:
[
{
"day": "Monday",
"startTime": "09:00",
"endTime": "17:00",
},
{... //Other days}
]
I want to unwind the availability array.
I have tried this:
let startTime = await Booking.aggregate([
{
$match: { _id: new mongoose.Types.ObjectId(req.params.id) },
},
{
$lookup: {
from: "parkings",
localField: "parkingId",
foreignField: "_id",
as: "parking",
},
},
{
$unwind: "$parking",
},
{ $unwind: "$parking.availability" },
]);
But it logs the result 6 times because there are 6 objects in the availability array like following.
[{
_id: new ObjectId("62e8ac3943a6bfaf80de75b5"),
parkingId: new ObjectId("62e11ab3079daa939290fa07"),
user: new ObjectId("62c950dfc96c2b690028be88"),
date: 2022-07-26T00:00:00.000Z,
startTime: 2022-07-26T09:30:00.000Z,
duration: { days: 0, hours: 2, minutes: 0 },
endTime: 2022-07-26T11:30:00.000Z,
isFeePaid: false,
status: 'sent',
isBookingCancelled: { value: false },
isStarted: false,
isEnabled: false,
parking: {
_id: new ObjectId("62e11ab3079daa939290fa07"),
userId: new ObjectId("62c950dfc96c2b690028be88"),
contactInfo: [Object],
about: 'Nisi occaecat ipsum',
parkingImage: [],
location: [Array],
price: 5,
availability: [Object],
parkingType: 'residence',
parkingInfo: [Array],
totalSpots: 10,
coordinates: [Object],
status: 'active',
isFeePaid: false,
}
},
]
I want only availability data but it prints availability: [Object]
I have an array of grades in mongoose, with students inside each grade nested in that array. I've been trying to sort the number of laps each student has inside of one array, but the sort function isn't sorting them; it leaves them in the formation they were in.
[
name: 'Grade A',
user: 625b2ea7a921450275bd9500,
students: [
{
_id: 625d8f55a856e935cebe070a,
name: 'OB',
laps: 18,
time: 2022-05-15T15:28:39.734Z,
avgSpeed: 307.32371093750004,
topSpeed: 663.36
},
{
_id: 625d8f5ca856e935cebe070b,
name: 'CB',
laps: 7,
time: 2022-05-15T15:20:33.618Z,
topSpeed: 9562.13,
avgSpeed: 944.2103125
},
{
_id: 625d8f62a856e935cebe070c,
name: 'CC',
laps: 5,
time: 2022-05-14T19:59:31.637Z,
avgSpeed: 3.245,
topSpeed: 6.49
},
{
_id: 625d8f67a856e935cebe070d,
name: 'JC',
laps: 6,
time: 2022-05-14T19:57:05.646Z,
avgSpeed: 516.751875,
topSpeed: 7128.13
},
{
_id: 625d8f6aa856e935cebe070e,
name: 'EE',
laps: 5,
time: 2022-05-15T15:18:13.486Z,
avgSpeed: 2904.3375,
topSpeed: 10317.03
},
{
_id: 625d8f70a856e935cebe070f,
name: 'LF',
laps: 3,
time: 2022-05-14T19:09:00.683Z
},
{ _id: 625d8f78a856e935cebe0710, name: 'RH', laps: 2 },
{
_id: 625d8f7ea856e935cebe0711,
name: 'AH',
laps: 22,
time: 2022-05-14T19:06:35.126Z
},
{ _id: 625d901b6922d33754e48622, name: 'SM' },
{ _id: 625d90206922d33754e48623, name: 'CP' },
{ _id: 625d90256922d33754e48624, name: 'SS' },
{
_id: 625d902b6922d33754e48625,
name: 'NT',
laps: 3,
time: 2022-05-15T15:26:21.236Z,
avgSpeed: 582.14,
topSpeed: 605.01
},
{
_id: 625d902f6922d33754e48626,
name: 'IZ',
laps: 3,
time: 2022-05-15T15:22:45.552Z,
topSpeed: 9117.38,
avgSpeed: 2569.3199999999997
},
{
_id: 6261d505122d0a073be3fc73,
name: 'Mr. T',
laps: 59,
time: 2022-05-15T15:14:43.743Z,
topSpeed: 997.58,
avgSpeed: 102.66416015625
}
],
topSpeed: [],
topLaps: [],
createdAt: 2022-04-18T16:18:10.578Z,
__v: 0
},
{
_id: 625d88094d09ed2e5090f91b,
name: 'Grade B',
user: 625b2ea7a921450275bd9500,
students: [
{
_id: 625d8f55a856e935cebe070a,
name: 'j',
laps: 18,
time: 2022-05-15T15:28:39.734Z,
avgSpeed: 307.32371093750004,
topSpeed: 663.36
},
{
_id: 625d8f5ca856e935cebe070b,
name: 'i',
laps: 7,
time: 2022-05-15T15:20:33.618Z,
topSpeed: 9562.13,
avgSpeed: 944.2103125
},
{
_id: 625d8f62a856e935cebe070c,
name: 'h',
laps: 5,
time: 2022-05-14T19:59:31.637Z,
avgSpeed: 3.245,
topSpeed: 6.49
},
{
_id: 625d8f67a856e935cebe070d,
name: 'h',
laps: 6,
time: 2022-05-14T19:57:05.646Z,
avgSpeed: 516.751875,
topSpeed: 7128.13
},
{
_id: 625d8f6aa856e935cebe070e,
name: 'j',
laps: 5,
time: 2022-05-15T15:18:13.486Z,
avgSpeed: 2904.3375,
topSpeed: 10317.03
},
{
_id: 625d8f70a856e935cebe070f,
name: 'iF',
laps: 3,
time: 2022-05-14T19:09:00.683Z
},
{ _id: 625d8f78a856e935cebe0710, name: 'H', laps: 2 },
{
_id: 625d8f7ea856e935cebe0711,
name: 'g',
laps: 22,
time: 2022-05-14T19:06:35.126Z
},
{ _id: 625d901b6922d33754e48622, name: 'd' },
{ _id: 625d90206922d33754e48623, name: 'f' },
{ _id: 625d90256922d33754e48624, name: 'e' },
{
_id: 625d902b6922d33754e48625,
name: 'c',
laps: 3,
time: 2022-05-15T15:26:21.236Z,
avgSpeed: 582.14,
topSpeed: 605.01
},
{
_id: 625d902f6922d33754e48626,
name: 'b',
laps: 3,
time: 2022-05-15T15:22:45.552Z,
topSpeed: 9117.38,
avgSpeed: 2569.3199999999997
},
{
_id: 6261d505122d0a073be3fc73,
name: 'a',
laps: 59,
time: 2022-05-15T15:14:43.743Z,
topSpeed: 997.58,
avgSpeed: 102.66416015625
}
],
topSpeed: [],
topLaps: [],
createdAt: 2022-04-18T15:47:21.875Z,
__v: 0
}
]
I've tried using something like this to sort, but it doesn't return the items in a sorted order.
let sorted = await Sensor.findById(req.params.id).sort({ laps: -1 }).exec();
console.log("sorted", sorted);
} catch (err) {
console.log(err);
}
Again, I'm only trying to sort the students by laps in a certain grade, not all of the students from all classes.
Thanks!
Have you tried:
let sorted = await Sensor.findById(req.params.id).sort({
"students.laps": -1
}).exec();
console.log("sorted", sorted);
}
catch (err) {
console.log(err);
}
I have a column setup with a cell formatter and is working fine.
{ title: "MKT Score", field: "MKTScore", width: 110, hozAlign: "center", formatter: "star", formatterParams: { stars: 3 }, },
what I'm look at trying to do is, if a value of a different cell is false is to hide the Formatter on a cell so it would be blank.
For some situations, such as the "star" formatter, you could pass a function to the formatterParams property in order to manupilate the formatter. Here is an example where, if the bool column is false, show blank:
const dataSet1 = [
{ id: 1, name: 'Billy Bob', bool: false, gender: 'male', rating: 2, col: 'red' },
{ id: 2, name: 'Mary May', bool: true, gender: 'female', rating: 3, col: 'blue' },
{ id: 3, name: 'Christine Lobowski', bool: false, gender: 'female', rating: 0, col: 'green' },
{ id: 4, name: 'Brendon Philips', bool: true, gender: 'male', rating: 2, col: 'orange' },
{ id: 5, name: 'Margret Marmajuke', bool: false, gender: 'female', rating: 0, col: 'yellow' }
]
const table = new Tabulator('#table', {
data: dataSet1,
columns: [
{ title: 'Name', field: 'name' },
{ title: 'Bool', field: 'bool' },
{ title: 'Gender', field: 'gender' },
{ title: 'Rating', field: 'rating', formatter: "star" , formatterParams: stars },
{ title: 'Favourite Color', field: 'col' }
]
})
function stars(row) {
return row.getData().bool ? { stars: 3 } : { stars: -1 }
}
<link href="https://unpkg.com/tabulator-tables#5.1.8/dist/css/tabulator.min.css" rel="stylesheet">
<script type="text/javascript" src="https://unpkg.com/tabulator-tables#5.1.8/dist/js/tabulator.min.js"></script>
<div id="table"></div>
I am trying to do groupBy with a population operation in MongoDB with Mongoose and Node.js.
I have one separate collection which is holding client_id and project_id. One client can have many projects, that's why I have created another collection for better understanding:
var CustomerHasProjectSchema = new mongoose.Schema({
client_id: {type: mongoose.Schema.Types.ObjectId, ref: 'Customer'},
project_id: {type: mongoose.Schema.Types.ObjectId, ref: 'Project'},
txn_date: {
type: Date,
default: Date.now
},
status: {
type: Number,
default : 1
}
});
I am trying to do something like
groupby(client_id)
and populate(user_id)
The exact query I have tried is
return customerHasProject.aggregate(
[
{
$match: {
$and: [
{ client_id: id }
]
}
},
{
$group : {
_id : "$client_id",
count: {
$sum: 1
}
}
},
{
$lookup: {
from: "project",
localField: "project_id",
foreignField: "_id",
as: "projects"
}
},
{
$unwind : {
"path" : "$project_id"
}
}
]
I tried this with help from another StackOverflow post.
I have also tried:
return customer2.default.find({'_id' : req.params.id}).exec().then(response=>{
project2.default.find({'client_id' : response[0]._id}).populate('client_id').then(customerWithProject=>{
res.json(customerWithProject)
})
})
But this is returning client detail in every project with a result like:
[
{
_id: "5883b189872c7d00042e1630",
projectTitle: "Marketing Analytics",
client_id: {
_id: "58833d9e1079ba000445a3a1",
clientName: "Anil",
companyName: "fgjhtrjhed",
website: "jhtryjh",
clientEmail: "frhyrt#tyhtr.dfkgjoiu",
mobileNumber: 549821654,
companyLocation: "New Delhi, Delhi, India",
clientCategory: "Freelancer",
__v: 0,
status: 1,
txn_date: "2017-01-21T10:53:18.360Z"
},
start_date: "2017-01-21T18:30:00.000Z",
end_date: "2017-01-30T18:30:00.000Z",
domain: "Back to the Future",
proposed_amount: 138000,
expectedFinalAmount: 200000,
__v: 0,
status: 1,
txn_date: "2017-01-21T19:07:53.214Z",
keywords: [
"JavaScript",
"AngularJs",
"C"
]
},
{
_id: "5883b213872c7d00042e1631",
projectTitle: "Indus Milk",
client_id: {
_id: "58833d9e1079ba000445a3a1",
clientName: "Anil",
companyName: "fgjhtrjhed",
website: "jhtryjh",
clientEmail: "frhyrt#tyhtr.dfkgjoiu",
mobileNumber: 549821654,
companyLocation: "New Delhi, Delhi, India",
clientCategory: "Freelancer",
__v: 0,
status: 1,
txn_date: "2017-01-21T10:53:18.360Z"
},
domain: "Back to the Future",
start_date: "2017-01-01T18:30:00.000Z",
end_date: "2017-01-06T18:30:00.000Z",
proposed_amount: 9200,
expectedFinalAmount: 15000,
__v: 0,
status: 1,
txn_date: "2017-01-21T19:10:11.403Z",
keywords: [
"Java",
"JavaScript"
]
},
{
_id: "5883b817872c7d00042e1632",
proposed_amount: 120000,
start_date: "2017-01-09T18:30:00.000Z",
end_date: "2017-02-03T18:30:00.000Z",
domain: "Back to the Future",
client_id: {
_id: "58833d9e1079ba000445a3a1",
clientName: "Anil",
companyName: "fgjhtrjhed",
website: "jhtryjh",
clientEmail: "frhyrt#tyhtr.dfkgjoiu",
mobileNumber: 549821654,
companyLocation: "New Delhi, Delhi, India",
clientCategory: "Freelancer",
__v: 0,
status: 1,
txn_date: "2017-01-21T10:53:18.360Z"
},
projectTitle: "Something",
__v: 0,
status: 1,
txn_date: "2017-01-21T19:35:51.835Z",
keywords: [
"C++",
"C"
]
}
]
Expecting something like
[
{
client_id: {
_id: "58833d9e1079ba000445a3a1",
clientName: "Anil",
companyName: "fgjhtrjhed",
website: "jhtryjh",
clientEmail: "frhyrt#tyhtr.dfkgjoiu",
mobileNumber: 549821654,
companyLocation: "New Delhi, Delhi, India",
clientCategory: "Freelancer",
__v: 0,
status: 1,
txn_date: "2017-01-21T10:53:18.360Z"
},
project_id : [
{
_id: "5883b213872c7d00042e1631",
projectTitle: "Indus Milk",
domain: "Back to the Future",
start_date: "2017-01-01T18:30:00.000Z",
end_date: "2017-01-06T18:30:00.000Z",
proposed_amount: 9200,
expectedFinalAmount: 15000,
},{
_id: "5883b817872c7d00042e1632",
proposed_amount: 120000,
start_date: "2017-01-09T18:30:00.000Z",
end_date: "2017-02-03T18:30:00.000Z",
domain: "Back to the Future",
projectTitle: "Something",
expectedFinalAmount: 15000,
}
]
}
]