Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to solve `...` error in nodejs javascript

I am new to NodeJS. Here's the Code:

const a = [
    {
        wa_id: 1,
        wa_property_id: 'p1',
        wa_view_name: 'ram',
        wa_view_id:1
    },
    {
        wa_id: 1,
        wa_property_id: 'p1',
        wa_view_name: 'sam',
        wa_view_id:'v2'
    },
    {
        wa_id: 1,
        wa_property_id: 'p2',
        wa_view_name: 'kam',
        wa_view_id:'v3'
    },
    {
        wa_id: 2,
        wa_property_id: 'p5',
        wa_view_name: 'pri',
        wa_view_id:'v4'
    },
    {
        wa_id: 1,
        wa_property_id: 'p3',
        wa_view_name: 'ste',
        wa_view_id:'v5'
    },
];

var result = a.reduce((acc,rec) =>{
//if result object doesn't contain key for wa_id - add new id key
if(!(Object.keys(acc).includes(rec.wa_id.toString())))
{
  return {...acc, [rec.wa_id]: {[rec.wa_property_id]:{[rec.wa_view_id]:rec.wa_view_name}}}
}
//if result id object doesn't contain key for property - add new property key
if(!(Object.keys(acc[rec.wa_id]).includes(rec.wa_property_id.toString())))
{
    // acc[rec.wa_id] = {...acc[rec.wa_id],[rec.wa_property_id]:[rec.wa_view_name] }
    acc[rec.wa_id] = {...acc[rec.wa_id],[rec.wa_property_id]:{[rec.wa_view_id]:rec.wa_view_name} }
  return acc
}
//otherwise add new value to array of properties
acc[rec.wa_id][rec.wa_property_id][rec.wa_view_id] = rec.wa_view_name
return acc
},{})

console.log("Output: ",result)

I got a following error. The Error Image Below,

enter image description here

It's working on online Javascript Code Editor. But in my system, it shows the above error. After some search on Internet, I came to know that my nodejs doesn't support ... How to make it support. I am using the latest Node JS version(my node js version: v12.16.1). Help me with some solutions.

(base) paulsteven@smackcoders:~/data-filters/flax2.0/flax_back_end$ node -v
v12.16.1
like image 294
Cherri Smack Avatar asked Jul 20 '26 13:07

Cherri Smack


1 Answers

You will have to configure your babel script.

Step1 : Install module by using

npm install --save-dev babel-plugin-transform-object-rest-spread.

Step2: Add this in your babel config file

"plugins": [["transform-object-rest-spread", { "useBuiltIns": true }]]

like image 141
Niraj Patel Avatar answered Jul 23 '26 02:07

Niraj Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!