Separate hours from minutes in array of times [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm currently working on a project with Redux and React.
To fill in an input I have to request the server and the server returns an array of times(it may have one member or several).
like this:
{
"start_hour": [
"10:00",
"15:55",
"20:08"
]
}
I have to separate the hours and minutes and have two states.
like this:
{
"hours": [
"10",
"15",
"20"
];
"minutes": [
"00",
"55",
"08"
];
}
How can I do this? Are there any libraries for this?
Thanks for the Help.

Using Array.prototype.reduce and String.split function, this can be done as follows.
const input = {
"start_hour": [
"10:00",
"15:55",
"20:08"
]
};
const output = input.start_hour.reduce((acc, cur) => {
const curArr = cur.split(':');
acc.hours.push(curArr[0]);
acc.minutes.push(curArr[1]);
return acc;
}, { hours: [], minutes: [] });
console.log(output);

Related

javascript check if any array is empty from an object [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
There is an object
let response = {
"meta": {
"id": 781
},
"content": {
"title": "Test",
"min": "1",
"max": "30",
"series": [
{
"name": "A",
"data": []
},
{
"name": "B",
"data": [3, 5, 6]
}
]
}
}
I need to check if any array of key, data is an empty array from series array. So, basically
if any data is empty array from series {
// do something
}
How can I achieve that?
you can try this one
let temp = response.content.series;
let num_of_empty_arr=0;
for(let i=0;i<temp.length;i++){
let obj=temp[i];
if(obj.data.length==0)
num_of_empty_arr++;
}
console.log("totol empty arrays",num_of_empty_arr);
you can do this by looping through response.content.series
for(let i of response.content.series){
if(i.data.length === 0){
//do something
console.log(i.name)
}
}
some allows you to return a boolean (true or false) based on a condition. Use it on the series data and return a value based upon whether the data array has any elements.
let response={meta:{id:781},content:{title:"Test",min:"1",max:"30",series:[{name:"A",data:[]},{name:"B",data:[3,5,6]}]}};
let response2={meta:{id:781},content:{title:"Test",min:"1",max:"30",series:[{name:"A",data:[1,2]},{name:"B",data:[3,5,6]}]}};
function areSomeEmpty(data) {
return data.content.series.some(arr => !arr.data.length);
}
console.log(areSomeEmpty(response));
console.log(areSomeEmpty(response2));

Modified JSON response from nestjs while creating API [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I do have json like below
[
[{
"id": "4e181c1b-0a68-425e-9eb7-df36324b6cdb",
"date_actual": {
"value": "2020-10-21T13:15:00"
},
"Date": "20201021",
"name": "abc"
},
{
"id": "4e181c1b-0a68-425e-9eb7-df36324qqq",
"date_actual": {
"value": "2020-1-21T13:15:00"
},
"Date": "2020102",
"name": "xyz"
}
]
]
want to modified like below
[
{
"id": "4e181c1b-0a68-425e-9eb7-df36324b6cdb",
"date_actual": "2020-10-21T13:15:00",
"Date": "20201021",
"name": "abc"
},
{
"id": "4e181c1b-0a68-425e-9eb7-df36324qqq",
"date_actual": "2020-10-21T13:15:00",
"Date": "2020102",
"name": "xyz"
}
]
My service class code is
const rows = await bigqueryClient.query(queryData);
console.log("old json"+JSON.stringify(rows));
return rows;
how to solve this issue using nestjs any way please help me ?
This isn't specific to NodeJS or NestJS, but is regular old JavaScript. As you have an array of values you can use the Array.prototype.map method to map to a new array
return origArray.map((val) => ({ ...val, date_actual: val.date_actual.value }))

How to design nested 3 level json [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an object like below:
Parent: { Child1: [ {name:'grandchild1', value:'abc', checked:true}, {name:'grandchild2', value:'pqr', checked:false} ], Child2: [ {name:'grandchild3', value:'abcd', checked:false}, {name:'grandchild4', value:'pqrs', checked:true} ], parent2{...........}.... };
How can I make it nested JSON.
Just like in root: parent1, parent2...
Child: children1, ....( Corresponding to parent)
Grandchildren: based on children
Please guide me how can I make it?
To make a JSON object you just have to follow JSON syntax
In your example it will look like:
{
"parent": {
"child1": [{
"name": "grandchild1",
"value": "abc",
"checked": "true"
}, {
"name": "grandchild2",
"value": "pqr",
"checked": "false"
}]
}
}
Please note that in JSON, string values must be written with double quotes.
If you need to get JSON string from object programmatically, you can write in javascript:
var myJSON = JSON.stringify(obj);

Meteor: Convert MongoDB server code for client side [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I can fetch data from meteor mongo terminal using this code but can't fetch data from the client. I know for the client site need different syntax but I am new on this environment. How can I call from a client site? Thanks
db.events.aggregate([
{ "$project": {
"year": { "$year": "$date" },
"month": { "$month": "$date" },
"day": { "$dayOfMonth": "$date" }
}},
{ "$group": {
"_id": null,
"distinctDate": { "$addToSet": { "year": "$year", "month": "$month", "day": "$day" }}
}}
])
Currently Minimongo does not support aggregation.
But you can give a shot to meteorhacks:aggregate
Edit
Usage example:
meteor add meteorhacks:aggregate Then simply use .aggregate function
like below.
var metrics = new Mongo.Collection('metrics');
var pipeline = [
{$group: {_id: null, resTime: {$sum: "$resTime"}}}
];
var result = metrics.aggregate(pipeline);

Getting values from JSON [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have the following Json
{
"title": "my title",
"children":
[
{
"id":1,
"value1": 1
"value2": 2
},
{
"id":1,
"value1": 1
"value2": 2
},
]
}
How can in Angular, iterate trough the values, and get only value1 + value2?
You can just extract value1 and value2 from children array in your controller:
$scope.items = obj.children.map(function(r){ return {value1: r.value1, value2: r.value2 };
and then loop through items in your views
<div ng-repeat="item in items">
{{item.value1}} {{item.value2}}
</div>

Categories

Resources