These are chat archives for ramda/ramda

27th
Mar 2018
Adam Szaraniec
@mimol91
Mar 27 2018 05:54
This message was deleted
YannisMarios
@YannisMarios
Mar 27 2018 15:12
Hi. how to delete/omit object in nested array based on path?
Mike Lambert
@lax4mike
Mar 27 2018 15:12
maybe R.dissocPath
YannisMarios
@YannisMarios
Mar 27 2018 15:14
for example:
let obj1 = {
name: 'Jon',
contacts: [
{
id: 1,
name: 'Nick'
},
{
id: 2,
name: 'Yannis'
}
]
}
I want to delete first object from contacts
I have been struggling with it the whole day
Thanks I will try dissocPath
Mike Lambert
@lax4mike
Mar 27 2018 15:16
@ram-bot
let obj1 = {
  name: "Jon",
  contacts: [
    {
      id: 1,
      name: "Nick"
    },
    {
      id: 2,
      name: "Yannis"
    }
  ]
}

R.dissocPath(["contacts", 0], obj1);
ram-bot
@ram-bot
Mar 27 2018 15:16
{ name: 'Jon', contacts: { '1': { id: 2, name: 'Yannis' } } }
YannisMarios
@YannisMarios
Mar 27 2018 15:17
What if contacts is deeply nested?
Mike Lambert
@lax4mike
Mar 27 2018 15:17
just add to the path
R.dissocPath(["deeply", "nested", "contacts", 0], obj1);
YannisMarios
@YannisMarios
Mar 27 2018 15:17
cool! let me try this on REPL
this worked for me even with '0' insted of 0 :-) dissocPath(['values', 'contacts', '0'], state)
Thank you very much @lax4mike
Mike Lambert
@lax4mike
Mar 27 2018 15:20
:thumbsup:
YannisMarios
@YannisMarios
Mar 27 2018 15:21
hey...what if I want to add a contact? assocPath?
Mike Lambert
@lax4mike
Mar 27 2018 15:22
you might want to use a R.lensPath in that case
YannisMarios
@YannisMarios
Mar 27 2018 15:22
I am already using lensPath for updating
Waqas Noor
@waqasnoor
Mar 27 2018 15:24
Hi. How can i copy behaviour of $unwind from mongo.
let obj = {name:’alpha’,score:[1,2,3]} unwind(’score’,obj) //[ {name:’alpha’,score:1},{name:’alpha’,score:2},{name:’alpha’,score:3}]
Mike Lambert
@lax4mike
Mar 27 2018 15:26
@YannisMarios
const contactsLens = R.lensPath(["contacts"])

const removed = R.over(contactsLens, R.drop(1), obj1);

const newContact = {
  id: 3,
  name: "Mike"
};

const added = R.over(contactsLens, R.append(newContact), obj1)
YannisMarios
@YannisMarios
Mar 27 2018 15:27
Thank you!
@lax4mike
Mike Lambert
@lax4mike
Mar 27 2018 15:32
@waqasnoor maybe something like this
const unwind = (key, obj) => {
  return R.map(R.compose(
    R.merge(obj),
    R.objOf(key))
  )(R.prop(key, obj))
}
YannisMarios
@YannisMarios
Mar 27 2018 16:01
A probem is that it turns contacts from an array of objects into an object of objects
This behaviour is really strange
Mike Lambert
@lax4mike
Mar 27 2018 16:13
i think that might just be the ram-bot
ohh, i know why actually
R.dissoc is made to work on Objects
you need a function that works on lists instead
like R.drop in my last code snippet
YannisMarios
@YannisMarios
Mar 27 2018 16:17
I used the last one with drop
YannisMarios
@YannisMarios
Mar 27 2018 16:47
I had to Object.values(contacts) to get the array after R.over
Mike Lambert
@lax4mike
Mar 27 2018 16:49
Hmm, that doesn't seem right
YannisMarios
@YannisMarios
Mar 27 2018 16:50
true..while in REPL this works ..it doesn't in my React project...same code
using "ramda": "^0.25.0", in my package.json
YannisMarios
@YannisMarios
Mar 27 2018 17:08
This worked for delete dissocPath(['values'].concat(name.split('.')), state)
my compnent state is {values: { contacts: [....
and name is contacts.0
YannisMarios
@YannisMarios
Mar 27 2018 17:15
But in either case, updating with lensPath and set or deleting a contact with dissocPath , contacts turns from an array of objects to an object of objects and I had to use Object.values(company.contacts)
Mike Lambert
@lax4mike
Mar 27 2018 17:26
i just did it in the console, and it's an Array
YannisMarios
@YannisMarios
Mar 27 2018 17:27
Yes, there seems to be something wrong when using this in React
because in REPL it works fine
Here is my React component state if anyone wants to experiment:
let state = {
  errors: {},
  values: {
  "id": "5aae9c9d5c550e457824e5a8",
  "index": 4,
  "enabled": true,
  "name": "TADE COMPANY",
  "email": "glop@ui.op",
  "address": "345 sdfgs",
  "zip": "36345",
  "phone": "54674567",
  "notes": "sdfgsdfg",
  "users": [],
  "area": {
    "id": "5aae8eca9c535d37c807d4a4",
    "index": 8,
    "name": "NEW YORK",
    "country": {
      "id": "5aae31d654dc4658f43c3cb9",
      "name": "United States",
      "__typename": "Country"
    },
    "__typename": "Area"
  },
  "tax_office": {
    "id": "5aada14ff2240f783d990d74",
    "index": 2,
    "name": "ΔΟΥ ΗΡΑΚΛΕΙΟΥ",
    "__typename": "TaxOffice"
  },
  "activity": {
    "id": "5aad7632b40f935bf8caaac1",
    "index": 1,
    "name": "ΞΕΝΟΔΟΧΕΙΟ",
    "__typename": "Activity"
  },
  "contacts": [
    {
      "id": "5aaede358618ef10004a9cb1",
      "index": 18,
      "job_title": {
        "id": "5aaec289189fa372191705e3",
        "__typename": "JobTitle"
      },
      "title": {
        "id": "5aad2fe374cc211bb80bedd5",
        "__typename": "Title"
      },
      "name": "xRrRZjeZAeVtpkg",
      "office_phone": "(714) 257-5506",
      "office_fax": "(327) 944-9377",
      "mobile": "(920) 720-6514",
      "emergency_phone": "(859) 304-1573",
      "email": "cupajpe@startaxi.gr",
      "__typename": "Contact"
    },
    {
      "id": "5aaede358618ef10004a9cb2",
      "index": 19,
      "job_title": {
        "id": "5aaec29a189fa372191705e4",
        "__typename": "JobTitle"
      },
      "title": {
        "id": "5aad2fda74cc211bb80bedd3",
        "__typename": "Title"
      },
      "name": "QlEAkNpELgZEwY",
      "office_phone": "(464) 338-1365",
      "office_fax": "(519) 558-8966",
      "mobile": "(750) 756-1223",
      "emergency_phone": "(879) 767-1892",
      "email": "maame@startaxi.gr",
      "__typename": "Contact"
    },
    {
      "id": "5aaede358618ef10004a9cb3",
      "index": 20,
      "job_title": {
        "id": "5aaec289189fa372191705e3",
        "__typename": "JobTitle"
      },
      "title": {
        "id": "5aad2fe374cc211bb80bedd5",
        "__typename": "Title"
      },
      "name": "KkbilNryksGz",
      "office_phone": "(415) 692-6485",
      "office_fax": "(339) 382-3048",
      "mobile": "(951) 423-9794",
      "emergency_phone": "(454) 327-2721",
      "email": "kiv@startaxi.gr",
      "__typename": "Contact"
    },
    {
      "id": "5aaede358618ef10004a9cb4",
      "index": 21,
      "job_title": {
        "id": "5aaec29a189fa372191705e4",
        "__typename": "JobTitle"
      },
      "title": {
        "id": "5aad2fda74cc211bb80bedd3",
        "__typename": "Title"
      },
      "name": "WmjqWOIMidKDlfA",
      "office_phone": "(985) 643-2030",
      "office_fax": "(317) 360-2978",
      "mobile": "(315) 526-9900",
      "emergency_phone": "(415) 371-2462",
      "email": "dofa@startaxi.gr",
      "__typename": "Contact"
    }
  ],
  "trading_name": "wertwe",
  "tax_code": "werwertw3563456",
  "created_at": "Sun Mar 18 2018 19:06:37 GMT+0200 (EET)",
  "__typename": "Company"
 }
};

let lens1 = lensPath(['values', 'contacts']);
over(lens1, drop(1), state);

//dissocPath(['values', 'contacts', '0'], state)
YannisMarios
@YannisMarios
Mar 27 2018 17:48
and thank you all for your precious time and effort
Gant Laborde
@GantMan
Mar 27 2018 23:43
Buddy of mine just did this solution, figured I'd share: https://goo.gl/f22jqG
If you see a better way let me know, but it's quite nice.