Right now I'm writing code as kind of a test for handling data. I'm using shorter arrays than what I'm actually going to be handling and I'm having trouble with it and I think I know whats wrong but it's kind of like brain fart and I can't actually find the problem. Any help is appreciated so thank you in advance.
if (!Object.prototype.hasOwnProperty('log')) {
Object.defineProperty(Object.prototype, 'log', {
get: function() {
console.log(this);
}
});
}
if (!Object.prototype.hasOwnProperty('getProps')) {
Object.defineProperty(Object.prototype, 'getProps', {
get: function() {
//console.log(Object.getOwnPropertyNames(this));
return Object.getOwnPropertyNames(this);
}
});
}
function appendItem(list, item) {
list.push(item);
}
var cities = ["Houston", "Dallas", "Rockwall", "Bossier", "Shreveport"];
var dates = ["monday", "monday", "monday", "monday", "monday"];
var states = ["Texas", "Texas", "Texas", "Louisiana", "Louisiana"];
function Setup(obj, sub, thisArg) {
obj.forEach(function(key, index) {
if ((this[key] == undefined)) {
this[key] = [sub[index]];
} else {
appendItem(this[key], sub[index]);
}
}, thisArg);
return obj;
}
var weather = {};
Setup(dates, states, weather);
weather.log;
Setup(states, cities, weather);
weather.log;
The result that I'm wanting is something that's accomplishes this:
weather = {monday:{Texas:["Houston", "Dallas", "Rockwall"], Louisiana:["Bossier", "Shreveport"]}};
but in the form of a loop or function because the actual data that I'm going to use when I figure this out is way larger. This is the actual output that I'm getting though:
if (!Object.prototype.hasOwnProperty('log')) {
Object.defineProperty(Object.prototype, 'log', {
get: function() {
console.log(this);
}
});
}
if (!Object.prototype.hasOwnProperty('getProps')) {
Object.defineProperty(Object.prototype, 'getProps', {
get: function() {
//console.log(Object.getOwnPropertyNames(this));
return Object.getOwnPropertyNames(this);
}
});
}
function appendItem(list, item) {
list.push(item);
}
var cities = ["Houston", "Dallas", "Rockwall", "Bossier", "Shreveport"];
var dates = ["monday", "monday", "monday", "monday", "monday"];
var states = ["Texas", "Texas", "Texas", "Louisiana", "Louisiana"];
function Setup(obj, sub, thisArg) {
obj.forEach(function(key, index) {
if ((this[key] == undefined)) {
this[key] = [sub[index]];
} else {
appendItem(this[key], sub[index]);
}
}, thisArg);
return obj;
}
var weather = {};
Setup(dates, states, weather);
weather.log;
Setup(states, cities, weather);
weather.log;
I couldn't work out what your code was doing, but based on your example output, you just need a single loop, and chain the values together.
eg..