Cherri Smack
Cherri Smack

Reputation: 105

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

Upvotes: 3

Views: 102

Answers (3)

f4z3k4s
f4z3k4s

Reputation: 1251

I know the original question is to setup rest operator in Node, but for those who would not want to configure Babel, they might use an equivalent which is available by default in Node:

const foo = { x: 'v1', y: 'v2' };
const bar = { z: 'v3' };

we want to use spread operator for merging the two objects, like:

const merged = { ...foo, ...bar };

but we don’t have rest operator in our toolkit, so we use what ... actually does behind the scene:

const merged = Object.assign({}, foo, bar);

It is worth mentioning that neither ... nor Object.assign() result in so called deep copies meaning that if you’d like to code in an immutable fashion, you should always nest these operators or use something that result in a deep copy like:

const merged = JSON.parse(JSON.stringify(Object.assign({}, foo, bar)));

This way, if your original objects (foo and bar in this scenario) had any non-primitive values, you can make sure you won’t mutate anything that you didn’t want to.

Excuse me for being this detailed, but I think it might help somebody reading this question and being new to the JS/TS ecosystem. :)

Upvotes: 0

Cherri Smack
Cherri Smack

Reputation: 105

Thanks @Niraj Patel, Worked but i don't have .bablerc file. so i created it manually.

For those, who don't have .bablerc file in the root folder of the project. Create it manually.

(base) paulsteven@smackcoders:~/root_of_project/$ vim .babelrc

Add the content as the answer suggested above,

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

Save it and it's done....

Upvotes: 0

Niraj Patel
Niraj Patel

Reputation: 810

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 }]]

Upvotes: 6

Related Questions