Summarize data in array of objects by common date [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
This is my data,
"time" is number
[
{
"time": 202007150800,
"count": 10
},
{
"time": 202007160700,
"count": 11
},
{
"time": 202007160900,
"count": 12
}
]
How do I use "time" group data from 8 o'clock yesterday to 8 o'clock today and sum "count"
,For example, 7/15 data is 7/15 08:00 - 7/16 07:00
like this:
[
{
"time": 20200715,
"count": 21
},
{
"time": 20200716,
"count": 12
}
]

Try this function
function mapData(data) {
let result = [];
data.forEach(element => {
let t = element.time + 9200;
let substr = t.toString().substr(0, 8);
if(!result[substr])
result[substr] = 0;
result[substr] += element.count;
});
let returnResult = [];
result.forEach((element, index) =>
returnResult.push({
time: index,
count: element
})
);
return returnResult;
}

Related

Sorting array of objects including negative values [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 5 months ago.
The community is reviewing whether to reopen this question as of 4 months ago.
Improve this question
I have here the data
var sampleArray = [
{ "Rating 1": -75 },
{ "Rating 2": -70 },
{ "Rating 3": 98 },
{ "Rating 4": -88 },
{ "Rating 5": 29 },
];
I want to sort it to output the following
Highest rating
Lowest rating
Rating that is near 0 (if there are 2 results, the positive will be chosen example:-1,1 then 1 will be chosen)
I'm stuck on getting the second value only which is the rating. I've tried the following codes to check if I can get the rating, but it shows the entire value of array 0
function topProduct(productProfitArray) {
return productProfitArray[0];
}
You can do this with reduce and a bit of logic to keep track of your min,max and diff from zero items
var sampleArray = [
{ "Rating 1": -75 },
{ "Rating 2": -70 },
{ "Rating 3": 98 },
{ "Rating 4": -88 },
{ "Rating 5": 29 },
];
var result = sampleArray.reduce( (acc,i) => {
var val= Object.values(i)[0];
if(val > acc.max)
{
acc.maxEntry = i;
acc.max = val;
}
if(val < acc.min)
{
acc.minEntry = i;
acc.min = val;
}
var diffFromZero = Math.abs(0-val);
if(diffFromZero < acc.diffFromZero)
{
acc.diffFromZero = diffFromZero;
acc.diffEntry = i;
}
return acc;
},{min:Number.POSITIVE_INFINITY, max:Number.NEGATIVE_INFINITY,diffFromZero:Number.POSITIVE_INFINITY});
console.log("min",result.minEntry);
console.log("max",result.maxEntry);
console.log("closeToZero",result.diffEntry);

How we can replace the keys of an Object with new key? [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
I have an object like below
[
{
"day" : "monday",
"value": 1
},
{
"day" : "tuesday",
"value": 2
},
...
]
Is there any native javascript way to replace the key with a new key. Here I need to replace the "day" & "value" with "x" & "y" respectively. (like below)
[
{
"x" : "monday",
"y": 1
},
{
"x" : "tuesday",
"y": 2
},
...
]
you can use map and inside the call back create a new object and modify it as required. Then return this object
const data = [{
"day": "monday",
"value": 1
},
{
"day": "tuesday",
"value": 2
}
];
const newData = data.map((item) => {
const obj = {};
for (let keys in item) {
if (keys === 'day') {
obj['x'] = item[keys]
}
if (keys === 'value') {
obj['y'] = item[keys]
}
};
return obj
});
console.log(newData)

How to get keys from json object in javascript [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
I have a json file:
[
{
"name": "Cocktail 1",
"ingredients": {
"rum": 12,
"coke": 48
}
}, {
"name": "Cocktail 2",
"ingredients": {
"gin": 24,
"tonic": 60
}
}]
Now I want to get a list of the keys of each "name" object. At the end there should be ths list
var mydata[0] = rum
var mydata[1] = coke
var mydata[2] = gin
var mydata[3] = tonic
and save it into an array.
What i have tried
var mydata = JSON.parse("jsonstring").ingredients;
hope this is understanable?
for each data in the array (map)
you want the ingredient part (.ingredients),
extract the keys (Object.keys)
and flatten the array (.flat())
array.map(a => a.ingredients).map(a => Object.keys(a)).flat();
You may prefer loop style. the only difference is flattening occurs with ... operator.
var results = [];
for (let a of array) {
results.push(...Object.keys(a.ingredients))
}
My proposition is :
'use strict'
const array = [
{
"name": "Cocktail 1",
"ingredients": {
"rum": 12,
"coke": 48
}
},
{
"name": "Cocktail 2",
"ingredients": {
"gin": 24,
"tonic": 60
}
}
]
const mydata = array.map((val) => {
return Object.keys(val.ingredients);
}).flat();
console.log(mydata)
// expected output: Array ["rum", "coke", "gin", "tonic"]
// Now you can get :
var mydata[0] = rum
var mydata[1] = coke
var mydata[2] = gin
var mydata[3] = tonic
Hope that help you? Thank

How to group an array of objects by month & year? [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 have an array of objects like so:
entries: [
{
id:1,
text: "Lorem ipsum",
timeStamp:"Thu, 01 Jun 2018"
},
{
id:3,
text:"Lorem ipsum",
timeStamp:"Thu, 24 May 2018"
},
{
id:4,
text: "Lorem ipsum",
timeStamp:"Thu, 24 May 2018"
}
]
Now I'd like to be able to group them into an 'archive' array like so:
archive: [
{
monthyear: May 2018,
entries: 2
},
{
monthyear: June 2018,
entries: 5
}
]
Wondering what sort of array functions should I use to get the intended result.
You could use reduce method to group array items by date and toLocaleString method to get month and year.
const data = [{"id":1,"text":"Lorem ipsum","timeStamp":"Thu, 01 Jun 2018"},{"id":3,"text":"Lorem ipsum","timeStamp":"Thu, 24 May 2018"},{"id":4,"text":"Lorem ipsum","timeStamp":"Thu, 24 May 2018"}]
const result = data.reduce((r, {timeStamp}) => {
let dateObj = new Date(timeStamp);
let monthyear = dateObj.toLocaleString("en-us", { month: "long", year: 'numeric' });
if(!r[monthyear]) r[monthyear] = {monthyear, entries: 1}
else r[monthyear].entries++;
return r;
}, {})
console.log(Object.values(result))

How to convert JSON Array to HTML select (with checkboxes)? [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 8 years ago.
Improve this question
I have an array of json objects
[
{
"id": 1,
"name": "Max"
},
{
"id": 2,
"name": "Bob"
},
{
"id": 3,
"name": "Mike"
}
]
How to convert it to html select (with checkboxes)?
Here's a simple example that might help you:
<select name="datas" id="datas"></select>
<script>
html = "";
obj = {
"1" : "Name",
"2": "Age",
"3" : "Gender"
}
for(var key in obj) {
html += "<option value=" + key + ">" +obj[key] + "</option>"
}
document.getElementById("datas").innerHTML = html;
</script>
Hope this helps.
Keep me posted

Categories