I have a nested array with time values (in milliseconds). I want to create an object in case the array does not have an entry for that time value. Right now my code is like this:
var start = data[0].nDate; // time in milliseconds
var end = data[data.length-1].nDate; // time in milliseconds
var nestedData = d3.nest()
.key(function (d){ return d.zone}) // id
.key(function(d){return d.nDate}) // time in milliseconds
.rollup(function(v){return {
density: v[0].density,
nDate: v[0].nDate,
zone: v[0].zone,
type: "zone"
}})
.sortKeys(d3.ascending)
.sortValues(function(a,b){return a.date - b.date})
.entries(data);
for (var i=start;i<end; i++){
nestedData .forEach(function(b,j){
var oneZone = +b.values[j].key; // time in milliseconds
var o = {
type: "nozone",
zone: +b.key, // id
nDate: i,
density: 0,
};
if(+(oneZone) != i){
noZone.push(o)
}
})
}
//push calculations into a new array
nestedZones.forEach(function(d,i){
var oneZone = d.values;
for (var b=0; b<oneZone.length;b++){
var timeZone = (oneZone[b].value);
densityByZone.push(timeZone);
}
});
noZone.forEach(function(d){densityByZone.push(d)});
Right now, the conditional does not work. Sometimes, it creates entries for times that already have objects.
Right now, your code is using a nested loop in such a way that you'll create entries for objects that already have that "time". That's the expected behaviour if you use a forEach
inside a for loop that way.
Instead of that, I suggest you create an array with all the "times"...
var nDateArray = d3.range(start, end + 1, 1);
... and use a function like this to populate noZone
:
nestedData.forEach(function(e) {
nDateArray.filter(function(f) {
return e.values.map(function(g) {
return +g.key
}).indexOf(f) === -1;
}).forEach(function(h) {
noZone.push({
type: "nozone",
zone: +e.key, // id
nDate: h,
density: 0,
})
})
})
Here is a demo with that small array you shared in your comment :
var data = [{ date: "Sun Mar 26 2017 16:30:00", density: 0.04, nDate: 1656178, surface: 8000, zone: 99 }, { date: "Sun Mar 26 2017 17:00:00", density: 0.02, nDate: 1656180, surface: 2550, zone: 99 }]; var noZone = []; var start = data[0].nDate; // time in milliseconds var end = data[data.length - 1].nDate; // time in milliseconds var nDateArray = d3.range(start, end + 1, 1); var nestedData = d3.nest() .key(function(d) { return d.zone }) // id .key(function(d) { return d.nDate }) // time in milliseconds .rollup(function(v) { return { density: v[0].density, nDate: v[0].nDate, zone: v[0].zone, type: "zone" } }) .sortKeys(d3.ascending) .sortValues(function(a, b) { return a.date - b.date }) .entries(data); nestedData.forEach(function(e) { nDateArray.filter(function(f) { return e.values.map(function(g) { return +g.key }).indexOf(f) === -1; }).forEach(function(h) { noZone.push({ type: "nozone", zone: +e.key, // id nDate: h, density: 0, }) }) }) console.log(noZone)
<script src="https://d3js.org/d3.v4.min.js"></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.