Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

javascript - IE不支持传播运算符(...)如何解决此问题(IE does not support the spread operator (…) how to fix this issue)

    var cars = [{
    make: "audi",
    model: "r8",
    year: "2012"
    },
    {
    make: "audi",
    model: "rs5",
    year: "2013"
    },
    {
    make: "ford",
    model: "mustang",
    year: "2012"
    },
    {
    make: "ford",
    model: "fusion",
    year: "2015"
    },
    {
    make: "kia",
    model: "optima",
    year: "2012"
    }];    
var group = cars.reduce((r, a) => {
        console.log("a", a);
        console.log('r', r);
r[a.make] = [...r[a.make] || [], a];
return r;
}, {});

I want to make it a group of an array but This code doesn't work in IE so can anyone help me out to make it work in IE browser.(我想使其成为一组数组,但是此代码在IE中不起作用,因此任何人都可以帮助我使其在IE浏览器中起作用。)

  ask by Dinesh ghimire translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The modified answer address two issues with IE.(修改后的答案解决了IE的两个问题。)

IE does not support arrow functions(IE不支持箭头功能) IE does not support the spread operator(IE不支持点差运算符) So the logic is pretty simple.(因此逻辑很简单。) If the make key is not in the result, put a new empty array in the result for the key.(如果make键不在结果中,请在该键的结果中放入一个新的空数组。) And then just push the value to it.(然后将价值推向它。) var cars = [{ make: "audi", model: "r8", year: "2012" }, { make: "audi", model: "rs5", year: "2013" }, { make: "ford", model: "mustang", year: "2012" }, { make: "ford", model: "fusion", year: "2015" }, { make: "kia", model: "optima", year: "2012" } ]; var group = cars.reduce(function(r, a) { r[a.make] = (r[a.make] || []); r[a.make].push(a); return r; }, {}); console.log(group);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...