人力検索はてな
モバイル版を表示しています。PC版はこちら
i-mobile

こんにちは。JavaScript初心者です。jQueryを使った構文でIE8以下でエラーが出てしまい困っています。自分ではどうしてもうまくいかないので、どなたかご教授いただけないでしょうか。
以下のコードのreduceプロパティがIE8ではサポートされていないようなのですが、他に書き方はあるのでしょうか。どうかよろしくお願いします。


function parseString(str) {
return String(str)
.split(/&|;/)
.reduce(function(ret, pair) {
try{
pair = decodeURIComponent(pair.replace(/\+/g, ' '));
} catch(e) {
// ignore
}

var eql = pair.indexOf('='),
brace = lastBraceInKey(pair),
key = pair.substr(0, brace || eql),
val = pair.substr(brace || eql, pair.length),
val = val.substr(val.indexOf('=') + 1, val.length);

if ('' == key) key = pair, val = '';

return merge(ret, key, val);
}, { base: {} }).base;
}

●質問者: 匿名質問者
●カテゴリ:ウェブ制作
○ 状態 :終了
└ 回答数 : 1/1件

▽最新の回答へ

1 ● 匿名回答1号
ベストアンサー

以下をコピペしとけばreduceが使えるようになります。

if (!Array.prototype.reduce) {
 Array.prototype.reduce = function reduce(accumulator){
 if (this===null || this===undefined) throw new TypeError("Object is null or undefined");
 
 var i = 0, l = this.length >> 0, curr;
 
 if(typeof accumulator !== "function") // ES5 : "If IsCallable(callbackfn) is false, throw a TypeError exception."
 throw new TypeError("First argument is not callable");
 
 if(arguments.length < 2) {
 if (l === 0) throw new TypeError("Array length is 0 and no second argument");
 curr = this[0];
 i = 1; // start accumulating at the second element
 }
 else
 curr = arguments[1];
 
 while (i < l) {
 if(i in this) curr = accumulator.call(undefined, curr, this[i], i, this);
 ++i;
 }
 
 return curr;
 };
}

reduce | Mozilla Developer Network


匿名質問者さんのコメント
ありがとうございました。無事動作しました。もっと勉強します。
関連質問

●質問をもっと探す●



0.人力検索はてなトップ
8.このページを友達に紹介
9.このページの先頭へ
対応機種一覧
お問い合わせ
ヘルプ/お知らせ
ログイン
無料ユーザー登録
はてなトップ