-1

converting the array of object with duplicates in it using ramda

    var testData = [
    {"id" : "001",
        "project" : "one",
        "projectstartDate" : "10/12/2018"
    },{"id" : "001",
        "project" : "two",
        "projectstartDate" : "10/14/2018"
    },{"id"  : "002",
        "project" : "one",
        "projectstartDate" : "10/12/2018"
    },{"id"  : "002",
        "project" : "two",
        "projectstartDate" : "10/14/2018"
    }
]

need to get output as

{
"001" : {
    "one" : {
        "projectstartDate" : "10/12/2018"
    },
    "two" : {
        "projectstartDate" : "10/14/2018"
    }
},
"002" :{
    "one" : {
        "projectstartDate" : "10/12/2018"
    },
    "two" : {
        "projectstartDate" : "10/14/2018"
    }
}

creating object with id as key and removing duplicates

sayomaka
  • 11
  • 1
  • 3
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific problem you're having in a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Oct 12 '18 at 06:23
  • Tried couple of functions from ramda pipe, filter, map, lens but not getting the desired output – sayomaka Oct 12 '18 at 06:33
  • Have a look at `R.groupBy(R.prop('id'))` and then see if you can modify the grouped values into the format you’re after. – Scott Christopher Oct 12 '18 at 07:01

1 Answers1

2

Let's break this up into a few steps.

First, when you have a list of things that you want to group together by some feature, something like R.groupBy can be helpful. In this particular instance you want to group by the id property, so R.prop('id') can be used as the function given to R.groupBy.

const testData = [
    {"id" : "001",
        "project" : "one",
        "projectstartDate" : "10/12/2018"
    },{"id" : "001",
        "project" : "two",
        "projectstartDate" : "10/14/2018"
    },{"id"  : "002",
        "project" : "one",
        "projectstartDate" : "10/12/2018"
    },{"id"  : "002",
        "project" : "two",
        "projectstartDate" : "10/14/2018"
    }
]

console.log(R.groupBy(R.prop('id'), testData))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

Once you have these grouped, you want to convert a list in an object indexed by some value, which is often a good time to reach for R.indexBy. Because we want to index each list associated with each of the grouped object's keys, we'll need to R.map over the object.

const testData = {
  "001": [
    {
      "id": "001",
      "project": "one",
      "projectstartDate": "10/12/2018"
    },
    {
      "id": "001",
      "project": "two",
      "projectstartDate": "10/14/2018"
    }
  ],
  "002": [
    {
      "id": "002",
      "project": "one",
      "projectstartDate": "10/12/2018"
    },
    {
      "id": "002",
      "project": "two",
      "projectstartDate": "10/14/2018"
    }
  ]
}

console.log(
  R.map(R.indexBy(R.prop('project')), testData)
)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

Then we have to clean up the left over properties left in the nested object. You've got a couple of options here, namely by either declaring which properties want to remove with R.omit or which properties you want to keep with R.pick.

const testData = {
  "id" : "001",
  "project" : "one",
  "projectstartDate" : "10/12/2018"
}

console.log(
  R.pick(['projectstartDate'], testData)
)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

Now all that's left is to join it all together with a pipeline using R.pipe and R.map.

const testData = [
    {"id" : "001",
        "project" : "one",
        "projectstartDate" : "10/12/2018"
    },{"id" : "001",
        "project" : "two",
        "projectstartDate" : "10/14/2018"
    },{"id"  : "002",
        "project" : "one",
        "projectstartDate" : "10/12/2018"
    },{"id"  : "002",
        "project" : "two",
        "projectstartDate" : "10/14/2018"
    }
]

const fn = R.pipe(
  R.groupBy(R.prop('id')),
  R.map(R.pipe(
      R.indexBy(R.prop('project')),
      R.map(R.pick(['projectstartDate']))
  ))
)

console.log(
  fn(testData)
)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
Scott Christopher
  • 6,458
  • 23
  • 26