单个阵列成多个阵列列表

时间:2017-03-14 03:39:13

标签: javascript arrays node.js

有人可以帮我转过来吗

entry = [
  "start":   "07:00 pm",
  "end":     "07:45 pm",
  "rule":    "MO,WE", <<<<
  "summary": "test1"
];

成:

array = [
  {
    "start":   "07:00 pm",
    "end":     "07:45 pm",
    "rule":    "MO,WE",
    "summary": "test1",
    "day":     "WE" <<<<
  },
  {
    "start":   "07:00 pm",
    "end":     "07:45 pm",
    "rule":    "MO,WE",
    "summary": "test1",
    "day":     "WE" <<<< (I get this but instead it should be "MO")
  }
];    

这是我尝试获取数组的代码......但它没有工作......

var temprule = entry.rule; //"MO,WE"
var rulest =[];
rulest = temprule.split(',');
for(var j = 0; j < rulest.length; j++) {        
     var obj1 = Object.assign(rulest[j]);
     entry.day = obj1;     //getting only WE each time when the array passes
     jsondata.push(entry); // it has all elements like start,end,summary,rule and we need to add day to this...
}  

我不确定为什么它会在一天中添加相同的值,尽管它通过所有不同的规则循环。这可以通过代码实现吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

&#13;
&#13;
// first of all this should be an object not an array
var entry = {
  "start": "07:00 pm",
  "end": "07:45 pm",
  "rule": "MO,WE",
  "summary": "test1"
};

// map the rules into a new aray
var result = entry.rule.split(',').map(function(r) {
    var o = Object.assign({}, entry); // clone the object entry
    o.day = r;                        // and set the clone's day to r
    // uncomment the following line if you want to remove the property rule from the resultant objects
    // delete o.rule;                 // remove the property rule from the clone
    return o;
});

console.log(result);
&#13;
&#13;
&#13;

解释您的代码有什么问题:

  1. entry不是有效对象(但这不是主要问题)。
  2. Object.assign的语法是Object.assign(target, ...sources),但您将目标指定为字符串rulest[j]且没有来源,因此返回的值是与字符串具有相同内容的字符串rulest[j]
  3. 然后设置对象day的属性entry,并在每次迭代时反复推送它,并且因为对象通过引用传递 而不是,数组的所有项都将引用entry引用的同一对象。更改其中一个将改变所有其他,因此从技术上讲,您将使所有项目都是同一个对象,并且该对象将属性day设置为最后一个规则。您需要做的是使用对entry的正确调用克隆对象Object.assign,然后将其属性day设置为当前规则[,并可能删除rule属性,因为它不需要]然后推送克隆对象而不是entry对象。
  4. 我上面提到的传递参考的例子:

    &#13;
    &#13;
    var a = {
      "foo": "bar"
    };
    
    var b = a;            // not copied (a and b are the same object)
    
    // PROOF:
    b.stack = "overflow"; // here we change b (add the property stack to b)
    
    console.log(a);       // but yet a is changed too (even if we never did a.stack = ...)
    &#13;
    &#13;
    &#13;

    我的代码说明:

    1. rule拆分,属性以获取一系列规则。
    2. 使用Array.prototype.map映射该规则数组。 map将在数组的每个项上调用一个函数(它的回调)并获取该函数的返回值并将其推入一个新数组,并且当源数组中没有剩余项时(规则数组) ),它返回生成的数组,其长度相同。
    3. 对于每个项目(规则),我们使用entry克隆对象Object.assign(目标是新的空对象{},当然源是entry) 。然后,我们将其属性day分配给当前规则r并返回该对象,以便map将其推入将在工作完成后重新调整的数组中。 / LI>

      修改

      是的,你可以!您必须循环遍历数组并拆分对象并将它们推入结果数组中。您可以使用Array.prototype.reduce来简化这样的工作:

      &#13;
      &#13;
      function convert(arr) {                       // take a array of objects arr and return a new array of objects
        return arr.reduce(function(result, obj) {   // for each object obj in the array arr
          obj.rule.split(",").forEach(function(r) { // get its rule, split it by ',' and for each r in those parts
            var o = Object.assign({}, obj);         // clone the object obj
            // delete o.rule;                       // remove rule from the resulatant object o (uncomment if you want to)
            o.day = r;                              // set its day property to be the part r (the current part of the splitted rule)
            result.push(o);                         // add the object to the result array
          });
          return result;                            // return the result so we can push to it more object (see the docs of reduce)
        }, []);                                     // initialize the result array with an empty array [] (see the docs of reduce)
      }
      
      var array = [
        {"start": "07:00 pm",  "end": "07:45 pm",  "rule": "MO,WE", "summary": "test1"},
        {"start": "07:00 pm",  "end": "07:45 pm",  "rule": "TU,TH", "summary": "test1"}
      ];
      
      console.log(convert(array));
      &#13;
      &#13;
      &#13;

      MDNrecude的文档。

答案 1 :(得分:0)

试试这个

var temprule = entry.rule; //"MO,WE"
var temprule_split = entry.rule.split(','); //split it
var rulest =[];
rulest = temprule.split(',');
for(var j = 0; j < rulest.length; j++) {        
     var obj1 = Object.assign(rulest[j]);
     obj1.day=temprule_split[j];
     entry.day = obj1;     
     jsondata.push(entry); 
}