【问题标题】:I'm having trouble making an object where its properties are objects(and their properties are objects...so on)我在制作一个其属性是对象的对象时遇到问题(并且它们的属性是对象......等等)
【发布时间】:2021-03-20 04:45:03
【问题描述】:

现在我正在编写代码作为处理数据的一种测试。我使用的数组比我实际要处理的数组短,我遇到了麻烦,我想我知道出了什么问题,但这有点像脑放屁,我实际上找不到问题。感谢您提供任何帮助,因此在此先感谢您。

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;

我想要的结果是可以做到这一点的:

weather = {monday:{Texas:["Houston", "Dallas", "Rockwall"], Louisiana:["Bossier", "Shreveport"]}};

但是以循环或函数的形式,因为当我弄清楚这一点时我将使用的实际数据要大得多。这是我得到的实际输出:

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;

【问题讨论】:

    标签: javascript arrays javascript-objects nested-object


    【解决方案1】:

    您可以以不同的方式显示输出,例如将 vars 传递给 console.log():

    console.log(天气);

    或者通过漂亮的格式化:

    console.log(JSON.stringify(weather));

    如果你想继续,你应该注意输入数组中的数据如何, 如果它导致作为树或图。在分析您的情况后,一个可能的解决方案,如果输入数据是深度 = 3 的树类型,我建议是:

    <script>
    function append_item(list, item) {
      list.push(item);
    }
    
    var dates = ["monday", "monday", "monday", "monday", "monday"];
    var states = ["Texas", "Texas", "Texas", "Louisiana", "Louisiana"];
    var cities = ["Houston", "Dallas", "Rockwall", "Bossier", "Shreveport"];
    
    
    function setup(obj, sub, this_arg) {
      obj.forEach(function(key, index) {
        if ((this[key] == undefined)) {
          this[key] = [sub[index]];
        } else if (!this[key].includes(sub[index])){
          append_item(this[key], sub[index]);
        }
      }, this_arg);
      return obj;
    }
    
    
    weather = {};
    setup(dates, states, weather);
    
    var sub_dict = {};
    setup(states, cities, sub_dict);
    
    var obj_values = Object.values(weather);
    for (i=0; i<obj_values.length; i++){
        var x=obj_values[i];
        for (j=0; j<obj_values[i].length; j++){
            var y=obj_values[i][j];
            obj_values[i][j] = {};
            obj_values[i][j][y] = sub_dict[y];
        }
    }
    console.log(JSON.stringify(weather));
    </script>
    

    这段代码的输出是:

    {"monday":[{"Texas":["Houston","Dallas","Rockwall"]},{"Louisiana":["Bossier","Shreveport"]}]}
    

    (请注意,此格式可根据需要进行缩减,但格式略有不同)。

    【讨论】:

      【解决方案2】:

      我无法弄清楚您的代码在做什么,但根据您的示例输出,您只需要一个循环,并将值链接在一起。

      例如..

      var cities = ["Houston", "Dallas", "Rockwall", "Bossier", "Shreveport"];
      var dates = ["monday", "monday", "monday", "monday", "monday"];
      var states = ["Texas", "Texas", "Texas", "Louisiana", "Louisiana"];
      
      
      var weather = {};
      
      for (var l = 0; l < cities.length; l += 1) {
        //day
        var d = weather[dates[l]] =
          weather[dates[l]] || {};  
        //state
        var s = d[states[l]] = 
          d[states[l]] || [];
        //cities
        s.push(cities[l]);
      }
      
      console.log(weather);

      【讨论】:

      • 这真的很接近,但是有没有一种方法可以使用递归函数呢?如果没有,我可以完成这项工作,但无论如何谢谢你
      猜你喜欢
      • 2020-03-30
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      • 2015-12-23
      • 2013-12-09
      • 1970-01-01
      • 2019-02-28
      相关资源
      最近更新 更多