var str
に値を代入すると、文字列をバイトに変換したいのですが、$scope.event[]
を割り当てると、エラーstr.charCodeAt is not a function
が発生します。$scope.event[0]
は、最初のインデックスのバイトだけを返します。配列の総バイト数を取得するにはどうすればよいですか?charCodeを使用して値を割り当てる方法
ctrl.js
$scope.event = ["lorem ipsum", "lorem ipsum","lorem ipsum"]
function jsonToArray() {
var total = 0;
var str = $scope.event;
var bytes = []; // char codes
var bytesv2 = []; // char codes
for (var i = 0; i < str.length; ++i) {
var code = str.charCodeAt(i);
bytes = bytes.concat([code]);
bytesv2 = bytesv2.concat([code & 0xff, code/256 >>> 0]);
}
var totalBytes = 0;
console.log('bytes', bytes.join(', '));
for (var i = 0 ; i < bytes.length ; i++) {
totalBytes += bytes[i];
totalCurrentBytes.push(totalBytes);
}
console.log('Total bytes', totalBytes);
formatBytes(totalBytes);
}
jsonToArray();
[配列$ scope.eventをループ...](http://stackoverflow.com/questions/1128388/looping-through-elements-of-an- array-in-java script) – jonhopkins