3

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
4
  • Does this answer your questions stackoverflow.com/questions/56003497/… ? Commented Apr 7, 2020 at 6:24
  • 1
    Exactly what version of Node are you using? The spread operator in object literals has been supported since 8.3.0 Commented Apr 7, 2020 at 6:24
  • can you show the output of node -v, FYI your code works fine for me/ Commented Apr 7, 2020 at 6:24
  • @Vivek Molkar, my node js version: v12.16.1 Commented Apr 7, 2020 at 6:25

3 Answers 3

6

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

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

Comments

0

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....

1 Comment

I'm glad it helped you. You're welcome. Happy Coding.
0

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. :)

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.