在JavaScript中构建模块化方法

时间:2015-05-14 02:10:45

标签: javascript algorithm

我有以下代码,它符合我的要求,但是,它不是模块化的,也不是通用的。例如,我可能有一百个stats个对象。有没有办法让它更通用?

实际上,在dataSeries中,我只有两个对象数组。我根据它们的颜色(red, green)对它们进行排序。因此,只初始化了四个stats个对象。

var stats1 = {data: []}
var stats2 = {data: []}
var stats3 = {data: []}
var stats4 = {data: []}


stats1.data.push(self.dataSeries[0].data.filter(function (x) { return x.color == "green" }))
stats2.data.push(self.dataSeries[0].data.filter(function (x) { return x.color == "red" }))
stats3.data.push(self.dataSeries[1].data.filter(function (x) { return x.color == "green" }))
stats4.data.push(self.dataSeries[1].data.filter(function (x) { return x.color == "red" }))

a=[{ data: stats1.data[0] }, { data: stats2.data[0] }, { data: stats3.data[0] }, { data: stats4.data[0] }];

3 个答案:

答案 0 :(得分:2)

清理它的最简单方法是将所有变量移动到数组中。以下是一种可能性的样本。

编辑:添加了对未知数量的颜色的支持

var stats = [], colors = [];

for(var i = 0; i < 25; i++) {
    stats.push({data: []});
}

colors = ["red", "green", "blue", "yellow"];
colorCount = colors.length;
for(var i = 0, len = stats.length; i < len; i++) {
    color = colors[i % colorCount];
    stats[i].data.push(self.dataSeries[0].data.filter(
        function (x) { return x.color == color }
    ));
}

答案 1 :(得分:2)

如果您知道数据的数量,那么您已经完成了所有工作:

stat.data

现在看来您正在创建一个无用的var numberOfData = 4; var a = []; for (var i = 0; i < numberOfData; i++) { var color = (i%2 === 0) ? 'green' : 'red'; var index = Math.floor(i/2); var d = self.dataSeries[index].data.filter(function (x) { return x.color == color }); a.push( {data: d} ); } 数组,如果是这样,您可以通过以下方式简化代码:

a

在这两种情况下,生成的colors数组将与您的示例中的数组相同。

更新如果您有更多颜色,可以将它们全部放在var numberOfData = 4; var colors = ['green', 'red', 'blue', 'pink', 'rainbow']; var a = []; for (var i = 0; i < numberOfData; i++) { var color = colors[i%colors.length]; var index = Math.floor(i/2); var d = self.dataSeries[index].data.filter(function (x) { return x.color == color }); a.push( {data: d} ); } 的数组中,并使用模数获取匹配的颜色:

$videoPath = "/path/to/file.mp4";

// Create a snippet with title, description, tags and category ID
// Create an asset resource and set its snippet metadata and type.
// This example sets the video's title, description, keyword tags, and
// video category.
$snippet = new Google_Service_YouTube_VideoSnippet();
$snippet->setTitle("Test title");
$snippet->setDescription("Test description");
$snippet->setTags(array("tag1", "tag2"));

// Numeric video category. See
// https://developers.google.com/youtube/v3/docs/videoCategories/list 
$snippet->setCategoryId("22");

// Set the video's status to "public". Valid statuses are "public",
// "private" and "unlisted".
$status = new Google_Service_YouTube_VideoStatus();
$status->privacyStatus = "public";

// Associate the snippet and status objects with a new video resource.
$video = new Google_Service_YouTube_Video();
$video->setSnippet($snippet);
$video->setStatus($status);

// Specify the size of each chunk of data, in bytes. Set a higher value for
// reliable connection as fewer chunks lead to faster uploads. Set a lower
// value for better recovery on less reliable connections.
$chunkSizeBytes = 1 * 1024 * 1024;

// Setting the defer flag to true tells the client to return a request which can be called
// with ->execute(); instead of making the API call immediately.
$client->setDefer(true);

// Create a request for the API's videos.insert method to create and upload the video.
$insertRequest = $youtube->videos->insert("status,snippet", $video);

// Create a MediaFileUpload object for resumable uploads.
$media = new Google_Http_MediaFileUpload(
    $client,
    $insertRequest,
    'video/*',
    null,
    true,
    $chunkSizeBytes
);
$media->setFileSize(filesize($videoPath));


// Read the media file and upload it chunk by chunk.
$status = false;
$handle = fopen($videoPath, "rb");
while (!$status && !feof($handle)) {
  $chunk = fread($handle, $chunkSizeBytes);
  $status = $media->nextChunk($chunk);
}

fclose($handle);

// If you want to make other calls after the file upload, set setDefer back to false
$client->setDefer(false);

答案 2 :(得分:2)

这可能会让您了解如何将其变为更通用的东西。

a=[{ data: stats1.data[0] }, { data: stats2.data[0] }, { data: stats3.data[0] }, { data: stats4.data[0] }];

pushColor = function(stats, pos, x, color) {
    stats.data.push(self.dataSeries[pos].data.filter(function (x) {
        return x.color === color;
    }))
}

var arrayLength = a.length;
for (var i = 0; i < arrayLength; i++) {
    pushColor(a[i].data, a[1].pos, a[i].color, "green");
}
相关问题