简体   繁体   中英

Why i have this error when run node index.js and how to fix it

My code run well on browser, however when run by node index.js I get this error :

let x = (agr[item.id] ??= { id: item.id, count: 0 });
                                     ^

SyntaxError: Unexpected token '?'

This is my code

var array  = [{"id":1,"count":100},{"id":2,"count":200},{"id":1,"count":200}]
const groupedData = array.reduce((agr,item)=>{
    let x = (agr[item.id] ??= { key: item.id, count:0 });
    x.count+=(item.count);
    return agr
},{});
const result = Object.entries(groupedData).reduce((agr, item) => {
    agr.push(item);
    return agr;
}, []); 
console.log(result);

Thanks for your attention

The Logical nullish assignment is a very new feature to JavaScript.

Your version of Node.js is too old to support it.

Upgrade to Node.js 15.0.0 or (preferably) newer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM