0

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

3
  • let x = (agr[item.id] ??= { key: item.id, count:0 }); should be let x = (agr[item.id] = { key: item.id, count:0 }); Commented Jun 17, 2021 at 9:58
  • My code run very well on browser. The result is 1 array: [{"1","key:1,count:300"},{"2","key:2,count:200"}] Commented Jun 17, 2021 at 10:03
  • 2
    ??= only supported in Node 15.x. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jun 17, 2021 at 10:10

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.