1

I have an array of javascript objects (of undefinite length).

var records =
[ 
  { query_result_id: 373,
    url: 'https://www.example1.com/hheyue',
    title: 'title1',
    uselesskey3: 'yo', 
    uselesskey4: 'ya',     
    max_email_nb_sent: 1 },
 { query_result_id: 375,
  url: 'https://www.example2.com',
  title: 'title2',
  uselesskey3: 'yo', 
  uselesskey4: 'ya',     
  max_email_nb_sent: null } 
 //and so on...]

I am today creating a new object called parametersObj (to keep records unchanged for immutability reasons) based on records by doing a certain number of things:

var parametersObj = records
      //remove useless keys      
      .map(({
        title,uselesskey3, uselesskey4, max_email_nb_sent,
        ...item
      }) => item)
      //add new needed keys
      .map(s => (
        {
          ...s,
          status: process.env.CONTEXT === "production" ? "prod" : "dev",
          triggered_at: new Date().toLocaleString()
        }
      ));

This works perfectly.

But what I am trying to do now is to add on the code above when I create parametersObj a method where before removing useless keys (the first .map above), I add a new key called email_nb for each javascript object which is equal either to :

  • 0 if max_email_nb_sent is null
  • max_email_nb_sent + 1 if max_email_nb_sent is not null

The expected value of parametersObj after we execute the script should be:

[ 
  { query_result_id: 373,
    url: 'https://www.example1.com/hheyue',
    title: 'title1',
    email_nb: 2,
    status: "prod",
    triggered_at: '2019-9-30 23:11:12'   
  },
 { query_result_id: 375,
  url: 'https://www.example2.com',
  title: 'title2',
  email_nb: 0,
  status: "prod",
  triggered_at: '2019-9-30 23:11:12' } 
 //and so on...]

I can use ES6 and prefer to if possible as usually more concise.

I tried this but this below did not work

var parametersObj = records
          //adding the new key email_nb          
         .map(s=> s["email_nb"] = s["max_email_nb_sent"] + 1)
         //remove useless keys      
          .map(({
            title,uselesskey3, uselesskey4, max_email_nb_sent,
            ...item
          }) => item)
          //add new needed keys
          .map(s => (
            {
              ...s,
              status: process.env.CONTEXT === "production" ? "prod" : "dev",
              triggered_at: new Date().toLocaleString()
            }
          ));

but then parametersObj is becoming just equal to [1, 1] which is very wrong...

Feedback on some answers

Tried:

var parametersObj = records
              //adding the new key email_nb          
             .map(item => 
               { ...item, 
                email_nb: item.max_email_nb_sent ? 
                item.max_email_nb_sent + 1 : 0
               }
             )

             //remove useless keys      
              .map(({
                title,uselesskey3, uselesskey4, max_email_nb_sent,
                ...item
              }) => item)
              //add new needed keys
              .map(s => (
                {
                  ...s,
                  status: process.env.CONTEXT === "production" ? "prod" : "dev",
                  triggered_at: new Date().toLocaleString()
                }
              ));

but I get error:

Uncaught SyntaxError: Unexpected token ...

1 Answer 1

1

The problem with your code is you are returning an object with only email_nb property. You have to return other existing properties as well.

var parametersObj = records    
     .map(item => ({ ...item,
        "email_nb": item.max_email_nb_sent ? item.max_email_nb_sent + 1 : 0
      }))
     .map(
       //remove useless keys      
     )     
     .map(s => (
       //add new needed keys
     ));

Live Example:

var records = [{
    query_result_id: 373,
    url: 'https://www.example1.com/hheyue',
    title: 'title1',
    uselesskey3: 'yo',
    uselesskey4: 'ya',
    max_email_nb_sent: 1
  },
  {
    query_result_id: 375,
    url: 'https://www.example2.com',
    title: 'title2',
    uselesskey3: 'yo',
    uselesskey4: 'ya',
    max_email_nb_sent: null
  }
];

var parametersObj = records
  .map(item => ({ ...item,
    "email_nb": item.max_email_nb_sent ? item.max_email_nb_sent + 1 : 0
  }))

console.log(parametersObj);

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

5 Comments

I tried but I get Uncaught SyntaxError: Unexpected token ...
Can you post the complete error code? Also update your question with records data you are using.
Sure but I simply copy pasted the block .map you suggested and added to my current query...
Just added it now
@Mathieu - Check out my edited answer and try again.

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.