How tu use reduce array to sort entries date here:
const entries = [
{date: 'lu'},
{date: 'lu'},
{date: 'ma'},
{date: 'ma'}
]
where output count occurence of date
const data = [
{x: 'lu', y: 2},
{x: 'ma', y: 2},
{x: 'me', y: 0},
{x: 'je', y: 0},
{x: 've', y: 0},
{x: 'sa', y: 0},
{x: 'di', y: 0},
]
Thanks for help :)
Two nested forEach will do. Because you need to have the x an y values for each days even if it does not appear in the entries. So reduce won't do.
So you need to set a starter data array of objects for each days... Then loop the entries against every days.
const entries = [
{date: 'lu'},
{date: 'lu'},
{date: 'ma'},
{date: 'ma'}
]
const data = [
{x: 'lu', y: 0},
{x: 'ma', y: 0},
{x: 'me', y: 0},
{x: 'je', y: 0},
{x: 've', y: 0},
{x: 'sa', y: 0},
{x: 'di', y: 0},
]
entries.forEach(entry => {
data.forEach(d => {
if(d.x === entry.date){
d.y++
}
})
})
console.log(data)
Related
I have the following code
const xPosition = coordinates.find(position => position.x === avoidObstacleX);
This returns me the coordinates {x: 26, y: 10} this is not wrong, but I have another coordinate that is the one I will like to output which is {x: 26, y: 11} Is there a way I can pass two parameters to the find method?
You could use two variables (not parameters to the find method itself), like you already use one:
function findObstacle(coordinates, avoidObstacleX, avoidObstacleY) {
return coordinates.find(position => position.x === avoidObstacleX
&& position.y === avoidObstacleY);
}
const xyPosition = findObstacle(coordinates, avoidObstacleX, avoidObstacleY);
But from the other answer I now learn that there are two interpretations of your question...
find only retrieves a single element, you need to use the filter method:
const coordinates = [ {x: 26, y: 10}, {x: 26, y: 11}, {x: 12, y: 34} ]
const avoidObstacleX = 26;
// returns [ {x: 26, y: 10}, {x: 26, y: 11} ]
const xPosition = coordinates.filter(position => position.x === avoidObstacleX);
To pass one value:
const coordinates= [
{x: 26, y: 10},
{x: 26, y: 11},
{x: 36, y: 6},
{x: 7,y: 8}
]
const avoidObstacleX=26;
let result = coordinates.filter(position=> {
return position.x === avoidObstacleX ;
})
console.log(result)
You can pass two values:
const coordinates= [
{x: 26, y: 11},
{x: 26, y: 11},
{x: 26, y: 11},
{x: 7,y: 8}
]
function find(avoidObstaclex,avoidObstacley){
let result= coordinates.filter(position=> {
return position.x === avoidObstaclex && position.y === avoidObstacley ;
})
return result;}
const avoidObstacleX=26;
const avoidObstacleY=11;
console.log(find(avoidObstacleX,avoidObstacleY))
var data = {
id: 1,
track: {
"1": [
{x: 10, y: 10},
{x: 11, y: 11},
{x: 12, y: 12}
],
"2": [
{x: 10, y: 10},
{x: 11, y: 11},
{x: 12, y: 12}
]
}
}
console.log(data.track);
var rev = data.track["1"].reverse();
console.log(rev);
How can i reverse every array inside "track" object? But I showed you above, that i am able to reverse array, by selecting it by key, but can i literally reverse every array inside "track" object?
Use Object.keys() to find all keys in your data structure
var data = {
id: 1,
track: {
"1": [
{x: 10, y: 10},
{x: 11, y: 11},
{x: 12, y: 12}
],
"2": [
{x: 10, y: 10},
{x: 11, y: 11},
{x: 12, y: 12}
]
}
}
var keys = Object.keys(data.track);
var count = keys.length;
for (var i=0;i<count;i++)
{
var rev = data.track[keys[i]].reverse();
console.log(rev);
}
It's simple. Just loop the data.track to get reverse result.
for (var i in data.track) {
console.log(data.track[i].reverse());
}
I'm a junior Web Developer, looking for some guidance solving a problem. Please excuse me if I'm missing anything integral, as this my first time posting here.
I have an array of some data returned like so:
[
{x: Date(1234), y: 0}
{x: Date(1235), y: 0}
{x: Date(1236), y: 300}
{x: Date(1237), y: 300}
{x: Date(1238), y: 300}
{x: Date(1239), y: 300}
{x: Date(1240), y: 300}
{x: Date(1241), y: 0}
{x: Date(1242), y: 0}
{x: Date(1243), y: 0}
]
If possible, I'd like to return a new array in which all consecutive 'y' values > 0 are summed. In the new array, the summed value should be associated with the first 'x' value of the summed items, like so:
[
{x: Date(1234), y: 0}
{x: Date(1235), y: 0}
{x: Date(1236), y: 1500}
{x: Date(1241), y: 0}
{x: Date(1242), y: 0}
{x: Date(1243), y: 0}
]
I'm thinking this will likely involve 'reduce,' but I'm a little unsure how to proceed. Any help would be greatly appreciated.
Thanks in advance!
Using reduce, you could do something like this: https://jsbin.com/leladakiza/edit?js,console
var input = [
{x: Date(1234), y: 0},
{x: Date(1235), y: 0},
{x: Date(1236), y: 300},
{x: Date(1237), y: 300},
{x: Date(1238), y: 300},
{x: Date(1239), y: 300},
{x: Date(1240), y: 300},
{x: Date(1241), y: 0},
{x: Date(1242), y: 0},
{x: Date(1243), y: 0},
];
var output = input.reduce(function (acc, val) {
var lastIndex = acc.length - 1;
if (val.y <= 0 || lastIndex < 0 || acc[lastIndex].y <= 0) {
acc.push(val);
} else {
acc[lastIndex].y += val.y;
}
return acc;
}, []);
I think you can use a reduce function like this.
var arr = [{
x: Date(1234),
y: 0
},
{
x: Date(1235),
y: 0
},
{
x: Date(1236),
y: 300
},
{
x: Date(1237),
y: 300
},
{
x: Date(1238),
y: 300
},
{
x: Date(1239),
y: 300
},
{
x: Date(1240),
y: 300
},
{
x: Date(1241),
y: 0
},
{
x: Date(1242),
y: 0
},
{
x: Date(1243),
y: 0
}
];
var yGreaterThanZero = null;
var aggregated = arr.reduce(function(acc, cur) {
if (cur.y > 0) {
if (!yGreaterThanZero) {
acc.push(cur);
yGreaterThanZero = cur;
} else {
yGreaterThanZero.y += cur.y;
}
} else {
acc.push(cur);
}
return acc;
}, []);
console.log(aggregated);
This is a very crude logic. We start recording when a value greater than 0 is obtained and push it at the end of the recording (value less than 0)
var a = [
{x: Date(1234), y: 0},
{x: Date(1235), y: 0},
{x: Date(1236), y: 300},
{x: Date(1237), y: 300},
{x: Date(1238), y: 300},
{x: Date(1239), y: 300},
{x: Date(1240), y: 300},
{x: Date(1241), y: 0},
{x: Date(1242), y: 0},
{x: Date(1243), y: 0},
{x: Date(1244), y: 200},
{x: Date(1245), y: 200},
{x: Date(1246), y: 200},
{x: Date(1247), y: 200},
]
var newA = [];
var recording = false;
var temp = {}
a.forEach(item => {
if (item.y > 0) {
recording = true;
if (temp.y) {
if(!temp.x) temp.x = item.x;
temp.y = temp.y + item.y
} else {
temp = item;
}
} else {
if (recording) newA.push(temp)
recording = false;
temp = {};
newA.push(item);
}
})
if (recording) newA.push(temp)
console.log(newA)
I want to console.log a value within an array of objects based on another specific value. For example, I have a simple array of objects. For all of the objects with v: 1, I want to print the z value.
var array = [
{v:1, z: 4},
{v:3, z: 8},
{v:4, z: 6},
{v:1, z: 4},
{v:2, z: 9},
{v:2, z: 3},
{v:4, z: 7},
{v:1, z: 5},
];
I tried something like for (array.v(1) => { console.log(array.z); }); but the syntax isnt correct. What is the correct syntax here?
Try something like this:
array.forEach( function(a) { if ( a.v == 1 ) console.log(a.z); } );
You need to add a if statement to your for each loop
Also you can use a filter function but this will print the object that meet your filter, not only the z value
var array = [
{v:1, z: 4},
{v:3, z: 8},
{v:4, z: 6},
{v:1, z: 4},
{v:2, z: 9},
{v:2, z: 3},
{v:4, z: 7},
{v:1, z: 5},
];
console.log('For Each')
array.forEach(o=>{ if(o.v == 1)console.log(o.z)})
console.log('Filter')
console.log(JSON.stringify(array.filter(o=>o.v==1)))
I need to sort the data of series from largest to smallest for every series.
Sample fiddle
series: [{
name: 'John',
data: [{
y: 1}, {y: 2}, {y: 3}, {y: 4}, {y: 5
}]
}, {
name: 'Jane',
data: [{
y: 5}, {y: 4}, {y: 3}, {y: 2}, {y: 1
}]
}, {
name: 'Joe',
data: [{
y: 5}, {y: 2}, {y: 3}, {y: 4}, {y: 1
}]
}]
You can use the sort function. It can be applied like that:
series.forEach(function(name){
name.data.sort(function (a,b) {
if(a.y < b.y) {
return 1;
} else if (a.y > b.y) {
return -1;
}
return 0;
});
});
To make the code more understandable you can create a series variable and then sort it before calling the highcharts function. This is demonstrated here.