私は提起された別の質問に対する解決策に取り組んでおり、解決策を思いつきましたが、それを行うためのよりエレガントな方法があると確信しています。次のように、すべての値がコンマで区切られた値の文字列であるオブジェクトがあるとします。
{ "action" : "goto,goto", "target" : "http://www.google.com,http://www.cnn.com" }
ただし、次のように、値を分離し、オブジェクトをオブジェクトの配列に分割したいと考えています。
[
{ "action" : "goto", "target" : "http://www.google.com" },
{ "action" : "goto", "target" : "http://www.cnn.com" }
]
私の解決策は次のとおりです。
var actions = obj.action.split(',');
var targets = obj.target.split(',');
// combined the actions and targets arrays
var combinedData = _.zip(actions, targets);
// go through the combinedData array and create an object with the correct keys
var commandList = _.map(combinedData, function(value) {
return _.object(["action", "target"], value)
});
これは私が望んでいることであり、ひどいものではありませんが、これを達成するためのよりスマートな方法はありますか?