有人可以帮我转过来吗
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...
}
我不确定为什么它会在一天中添加相同的值,尽管它通过所有不同的规则循环。这可以通过代码实现吗?
谢谢!
答案 0 :(得分:2)
// 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;
解释您的代码有什么问题:
entry
不是有效对象(但这不是主要问题)。Object.assign
的语法是Object.assign(target, ...sources)
,但您将目标指定为字符串rulest[j]
且没有来源,因此返回的值是与字符串具有相同内容的字符串rulest[j]
。day
的属性entry
,并在每次迭代时反复推送它,并且因为对象通过引用传递 而不是值,数组的所有项都将引用entry
引用的同一对象。更改其中一个将改变所有其他,因此从技术上讲,您将使所有项目都是同一个对象,并且该对象将属性day
设置为最后一个规则。您需要做的是使用对entry
的正确调用克隆对象Object.assign
,然后将其属性day
设置为当前规则[,并可能删除rule
属性,因为它不需要]然后推送克隆对象而不是entry
对象。我上面提到的传递参考的例子:
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;
我的代码说明:
rule
拆分,
属性以获取一系列规则。Array.prototype.map
映射该规则数组。 map
将在数组的每个项上调用一个函数(它的回调)并获取该函数的返回值并将其推入一个新数组,并且当源数组中没有剩余项时(规则数组) ),它返回生成的数组,其长度相同。entry
克隆对象Object.assign
(目标是新的空对象{}
,当然源是entry
) 。然后,我们将其属性day
分配给当前规则r
并返回该对象,以便map
将其推入将在工作完成后重新调整的数组中。 / LI>
醇>
修改强>
是的,你可以!您必须循环遍历数组并拆分对象并将它们推入结果数组中。您可以使用Array.prototype.reduce
来简化这样的工作:
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;
MDN上recude
的文档。
答案 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);
}