0

Here is my code:

  let datasets = data.map(
    item => ({
      "label": item.name,
      "data": item.prices.map(nested => ({
        if( /* Last row */ ){
          "x": new Date(nested.updatedAt),
          "y": Number(nested.price)
        } else {
          "x": new Date(nested.createdAt),
          "y": Number(nested.price)
        }
      }))
    })
  );

I cannot seem to add an if statement at all to data mapping, above is a sample of what I am aiming to achieve. I tried to do a console.log for a test, it doesn't seem to allow code inside of the data map. Although I found this stackoverflow doing something similar. Any advice?

Miles Collier
  • 370
  • 2
  • 17
  • What is "Last row"? – guest271314 Jul 23 '17 at 21:54
  • Sorry for not clarifying, last row placeholder text there representing I want to use `nested.updatedAt` and not `nested.createdAt`. Since it's not like a for loop I am struggling getting that value for checking inside, I am reading through all responses now. – Miles Collier Jul 23 '17 at 21:58
  • There is only one of two options that change `nested.updatedAt` or `nested.createdAt`. You can use conditional operator to pass either of the values within `new Date()` constructor – guest271314 Jul 23 '17 at 22:00

3 Answers3

2

a map needs a return statement. So its perfectly valid to do something like this:

let datasets = data.map((item) => {
    var obj = {};
    obj.label = item.name
    obj.data = item.prices.map((subItem, key) => {
        if (item.prices.length === key + 1) {
            return {x: new Date(subItem.updateAt), y: Number(subItem.price)};
        }

        return {x: new Date(subItem.createdAt), y: Number(subItem.price)}
    })

    return obj;
})

the return statement ends the current iteration within the map.

  • This is perfect, do you know if I could also return two objects at the same time in this scenario? I keep trying to do this, but I keep double nesting – Miles Collier Jul 23 '17 at 22:58
  • @MilesCollier you can nest how many maps you'd like to, but each map can only return one thing, like an object. if you want to do two maps in parallel you could look into async.js parallel (http://caolan.github.io/async/docs.html#parallel) – Lucas Reppe Welander Jul 23 '17 at 23:02
1

You can use conditional operator withing new Date() constructor to pass either nested.updatedAt or nested.createdAt

  let datasets = data.map(
    item => ({
      "label": item.name,
      "data": item.prices.map(nested => 
                ({
                  "x": new Date(lastrow ? nested.updatedAt : nested.createdAt),
                  "y": Number(nested.price)
                }) 
              )
    })
  );
guest271314
  • 1
  • 15
  • 104
  • 177
0

The arguments passed to .map are item,count,array

So you can have.....

item.prices.map((nested,count,array) => {
    if ( count ==  arr.length - 1 ) { /* last one */ 
        // do something for last one
    } else {
        // not last one
    } 
});
user3094755
  • 1,561
  • 16
  • 20
  • It seems I cannot use if statements at all inside the map, e.g. for that example I get `Unexpected token ==`. No idea why – Miles Collier Jul 23 '17 at 21:46
  • Is your environment supporting ES6?, Try `item.prices.map(function(nested) { if ( ) { } })`. PS. the other responses are right in that you need to return Objects. ie... `return { thing: true }` – user3094755 Jul 23 '17 at 21:52