0

I have two fixes, where the first fix that is dataMedical has an attribute called safeId and I need it to look for its name in the dataSafe array, they are associated by the id attribute that is safeId.

*Remembering that dataMedical can have N item as well as dataSafe.*

dataMedical = [
    {
       _id: 5da619b36aae5e7028fc27db,
       paciente: 5de31abf4c430918a39a7490,
       seguroAfiliado: 5d65da2f008b72055d17abfb,
       fecha: '2019-10-01T03:00:00.000Z',
       motivoConsulta: 'dww',
       __v: 0
     }
 ]


dataSafe = [ { _id: 5d65da2f008b72055d17abfb,
    nombre: 'La Seguridad',
    email: 'info@laseguridad.com.ve',
    numeroPoliza: '123456',
    pais: 'fdsaafsd',
    baremo: 'afdafds',
    actoQuirurgico: 'fsdasafd',
    __v: 0 },
 { _id: 5d62c609e7179a084ef359fd,
     nombre: 'Mappfre',
     email: 'info@mappfre.com',
     numeroPoliza: '098765',
     actoQuirurgico: 'acto2',
     baremo: 'rweq',
     pais: 'usa' } ]

I need as a result:

dataMedicalResult = [
    {
       _id: 5da619b36aae5e7028fc27db,
       paciente: 5de31abf4c430918a39a7490,
       seguroAfiliado: 5d65da2f008b72055d17abfb,
       fecha: '2019-10-01T03:00:00.000Z',
       motivoConsulta: 'dww',
       nameSafe: 'La Seguridad',
       email: 'info@laseguridad.com.ve',
       numeroPoliza: '123456',
       pais: 'fdsaafsd',
       baremo: 'afdafds',
       actoQuirurgico: 'fsdasafd',
    }
]

Try all code and again and answer me this:

https://ramdajs.com/repl/?v=0.26.1#?%5B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22%24__%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22strictMode%22%3A%20true%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22selected%22%3A%20%7B%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22getters%22%3A%20%7B%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22_id%22%3A%20%225da619b36aae5e7028fc27db%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22wasPopulated%22%3A%20false%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22activePaths%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22paths%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22_id%22%3A%20%22init%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22paciente%22%3A%20%22init%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22seguroAfiliado%22%3A%20%22init%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22fecha%22%3A%20%22init%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22motivoConsulta%22%3A%20%22init%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22__v%22%3A%20%22init%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22states%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22ignore%22%3A%20%7B%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22default%22%3A%20%7B%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22init%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22_id%22%3A%20true%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22paciente%22%3A%20true%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22seguroAfiliado%22%3A%20true%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22fecha%22%3A%20true%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22motivoConsulta%22%3A%20true%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22__v%22%3A%20true%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22modify%22%3A%20%7B%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22require%22%3A%20%7B%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22stateNames%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22require%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22modify%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22init%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22default%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22ignore%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22pathsToScopes%22%3A%20%7B%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22cachedRequired%22%3A%20%7B%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22session%22%3A%20null%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%24setCalled%22%3A%20%7B%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22emitter%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22_events%22%3A%20%7B%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22_eventsCount%22%3A%200%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22_maxListeners%22%3A%200%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%24options%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22skipId%22%3A%20true%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22isNew%22%3A%20false%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22willInit%22%3A%20true%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%22isNew%22%3A%20false%2C%0A%20%20%20%20%20%20%20%20%22_doc%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22_id%22%3A%20%225da619b36aae5e7028fc27db%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22paciente%22%3A%20%225de31abf4c430918a39a7490%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22seguroAfiliado%22%3A%20%225d65da2f008b72055d17abfb%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22fecha%22%3A%20%222019-10-01T03%3A00%3A00.000Z%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22motivoConsulta%22%3A%20%22dww%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22__v%22%3A%200%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%22%24locals%22%3A%20%7B%7D%2C%0A%20%20%20%20%20%20%20%20%22%24init%22%3A%20true%0A%20%20%20%20%7D%0A%5D

Current Answer: enter image description here

Code modified with values 'safes' and 'citasMedicas':

enter image description here

Result with POSTMAN:

enter image description here

enter image description here

  • Does this answer your question? [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – Liam Dec 03 '19 at 17:11

6 Answers6

0

Try this:

console.log(dataSafe.reduce((acc,el)=> {
  const dm = dataMedical.find(dam => dam["_id"] === el["_id"]);  
  return dm ? [...acc, Object.assign(el, dm)] : acc;
}, []))
Max
  • 1,020
  • 7
  • 13
  • I'm guessing that this is backward, that the OP wants all the items in `dataMedical` enhanced with their counterparts from `dataSafe`. This enhances the items in `dataSafe` instead. – Scott Sauyet Dec 03 '19 at 23:51
  • I copy the code in > https://ramdajs.com/repl/?v=0.26.1 and everything ok but in nodejs I do the > var R = require ('ramda'); Any suggestions? – Denis Alayza Dec 04 '19 at 02:19
0

Would something like this work? it searches for the given id in both arrays and then joins the two found objects

const dataMedical = [{
  "_id": "1",
  "pacient": "Jhon",
  "safeId": "1",
  "data": "2019-10-01T03:00:00.000Z",
  "consult": "dww",
  "__v": 0
}]


const dataSafe = [{
    "_id": "1",
    "nameSafe": "La Seguridad"
  },
  {
    "_id": "2",
    "nameSafe": "Mappfre"
  }
]

function findRecord(id) {
  const medical = dataMedical.find(r => r._id === id)
  const safe = dataSafe.find(r => r._id === id)
  return {
    ...medical,
    ...safe
  }

}
const dataMedicalResult = findRecord("1")
console.log(dataMedicalResult)
aprouja1
  • 1,800
  • 8
  • 17
0

You can use the Array find function to locate the correct dataSafe element. We will map over each element in dataMedical, and use the safeId to find the correct dataSafe. Then, we will copy over the nameSafe property to the safe.

let dataMedical = [
    {
        "_id": "1",
        "pacient": "Jhon",
        "safeId": "1",
        "data": "2019-10-01T03:00:00.000Z",
        "consult": "dww",
        "__v": 0
    }
]


let dataSafe = [
    {
        "_id": "1",
        "nameSafe": "La Seguridad"
    },
    {
        "_id": "2",
        "nameSafe": "Mappfre"
    }
]

let dataMedicalResult = dataMedical.map(dm => {
  let safe = dataSafe.find(e => e._id == dm.safeId);
  return safe
    ? {...dm, nameSafe: safe.nameSafe}
    : dm;
})

console.log(dataMedicalResult);

EDIT: It looks like the other answers are not looking at the safeId property to find the correct safe, so this should be the correct solution.

Brian
  • 1,860
  • 1
  • 9
  • 14
  • I copy the code in > https://ramdajs.com/repl/?v=0.26.1 and everything ok but in nodejs I do the > var R = require ('ramda'); Any suggestions? – Denis Alayza Dec 04 '19 at 02:19
0

This could help too, I think trying to make point free results in a more complicate approach...

const mergeWithDataSafe = (merger, data) => merger(
  (item) => R.mergeRight(item, R.path([item.safeId, 0], data)),
);

const aggregate = R.useWith(mergeWithDataSafe, [
  R.flip(R.map),
  R.groupBy(R.prop('_id')),
]);


const dataMedical = [
  {
    "_id": "1",
    "pacient": "Jhon",
    "safeId": "1",
    "data": "2019-10-01T03:00:00.000Z",
    "consult": "dww",
    "__v": 0
  }
];


const dataSafe = [
  {
    "_id": "1",
    "nameSafe": "La Seguridad"
  },
  {
    "_id": "2",
    "nameSafe": "Mappfre"
  }
];

console.log(
  aggregate(dataMedical, dataSafe),
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js" integrity="sha256-xB25ljGZ7K2VXnq087unEnoVhvTosWWtqXB4tAtZmHU=" crossorigin="anonymous"></script>
Hitmands
  • 13,491
  • 4
  • 34
  • 69
  • I copy the code in > https://ramdajs.com/repl/?v=0.26.1 and everything ok but in nodejs I do the > var R = require ('ramda'); Any suggestions? – Denis Alayza Dec 04 '19 at 02:19
0

You can use R.indexBy to convert the dataSafe array to a dictionary using _id as the key. Now you can map dataMedical and merge with the item with the same safeId from the safeDict:

const { curry, indexBy, prop, merge } = R;

const fn = curry((medical, safe) => {
  const safeDict = indexBy(prop('_id'), safe); // convert the safe array to a dictionary with _id as key
  
  return medical.map(o => merge(safeDict[o.safeId], o)); // map medical and merge with object with same _id from safeDict
});

const dataMedical = [{"_id":"1","pacient":"Jhon","safeId":"1","data":"2019-10-01T03:00:00.000Z","consult":"dww","__v":0}];
const dataSafe = [{"_id":"1","nameSafe":"La Seguridad"},{"_id":"2","nameSafe":"Mappfre"}];

const result = fn(dataMedical, dataSafe)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js" integrity="sha256-xB25ljGZ7K2VXnq087unEnoVhvTosWWtqXB4tAtZmHU=" crossorigin="anonymous"></script>

The same idea can be easily used without Ramda - create a Map from dataSafe, and then map dataMedical and get the an item from the safeDict using the safeId, and combine with current medical object using spread:

const fn = (medical, safe) => {
  const safeDict = new Map(safe.map(o => [o._id, o])); // convert the safe array to a dictionary with _id as key
  
  return medical.map(o => ({ ...o, ...safeDict.get(o.safeId) })); // map medical and merge with object with same _id from safeDict
};

const dataMedical = [{"_id":"1","pacient":"Jhon","safeId":"1","data":"2019-10-01T03:00:00.000Z","consult":"dww","__v":0}];
const dataSafe = [{"_id":"1","nameSafe":"La Seguridad"},{"_id":"2","nameSafe":"Mappfre"}];

const result = fn(dataMedical, dataSafe);

console.log(result);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • I copy the code in > https://ramdajs.com/repl/?v=0.26.1 and everything ok but in nodejs I do the > var R = require ('ramda'); Any suggestions? – Denis Alayza Dec 04 '19 at 02:20
  • Replace `const { curry, indexBy, prop, merge } = R;` with `const { curry, indexBy, prop, merge } = require('ramda')`. – Ori Drori Dec 04 '19 at 07:08
  • Please, if you can see the result you throw at me, edit the question and paste a link. – Denis Alayza Dec 05 '19 at 01:44
0

I might write it like this:

const enhance = (safe) => (
  med,
  {_id, ...rest} = safe .find (({_id}) => _id == med.safeId) || {}
) => ({...med, ...rest})

const enhanceAll = (safe) => R.map (enhance (safe))


const dataMedical = [{_id: "1", pacient: "Jhon", safeId: "1", data: "2019-10-01T03:00:00.000Z", consult: "dww", __v: 0}]
const dataSafe = [{_id: "1", nameSafe: "La Seguridad"}, {_id: "2", nameSafe: "Mappfre"}]

console .log (
  enhanceAll (dataSafe) (dataMedical)
)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

Note that this barely uses Ramda, using only the map function. I would never introduce Ramda just for that, but if I was already using Ramda, this is nicer than this native version, which requires extra ceremony because the additional parameters supplied to Array.prototype.map (index and array) interfere with the defaulted parameters in enhance:

const enhanceAll = (safe) => (meds) => meds .map ( med => enhance (safe) (med))

Update

With the changes made to the question, the search term has to change from safeId to seguroAfiliado, making the code look like

const enhance = (safe) => (
  med,
  {_id, ...rest} = safe .find (({_id}) => _id == med.seguroAfiliado) || {}
) => ({...med, ...rest})

const enhanceAll = (safe) => R.map (enhance (safe))

You can see this running in Ramda's REPL.

Update 2

I tried this in node, and it worked fine:

index.js:

const R = require('ramda');

const dataMedical = [
  {
     _id: '5da619b36aae5e7028fc27db',
     paciente: '5de31abf4c430918a39a7490',
     seguroAfiliado: '5d65da2f008b72055d17abfb',
     fecha: '2019-10-01T03:00:00.000Z',
     motivoConsulta: 'dww',
     __v: 0
   }
]

const dataSafe = [ { 
  _id: '5d65da2f008b72055d17abfb',
  nombre: 'La Seguridad',
  email: 'info@laseguridad.com.ve',
  numeroPoliza: '123456',
  pais: 'fdsaafsd',
  baremo: 'afdafds',
  actoQuirurgico: 'fsdasafd',
  __v: 0 
}, {
  _id: '5d62c609e7179a084ef359fd',
  nombre: 'Mappfre',
  email: 'info@mappfre.com',
  numeroPoliza: '098765',
  actoQuirurgico: 'acto2',
  baremo: 'rweq',
  pais: 'usa' 
} 
]

const enhance = (safe) => (
  med,
  {_id, ...rest} = safe .find (({_id}) => _id == med.seguroAfiliado) || {}
) => ({...med, ...rest})

const enhanceAll = (safe) => R.map (enhance (safe))

console .log (
  JSON.stringify(
    enhanceAll (dataSafe) (dataMedical), 
    null, 
    4
  )
)
> npm install ramda
[...]
> node --version
v10.15.0
> node index
[
    {
        "_id": "5da619b36aae5e7028fc27db",
        "paciente": "5de31abf4c430918a39a7490",
        "seguroAfiliado": "5d65da2f008b72055d17abfb",
        "fecha": "2019-10-01T03:00:00.000Z",
        "motivoConsulta": "dww",
        "__v": 0,
        "nombre": "La Seguridad",
        "email": "info@laseguridad.com.ve",
        "numeroPoliza": "123456",
        "pais": "fdsaafsd",
        "baremo": "afdafds",
        "actoQuirurgico": "fsdasafd"
    }
]
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • I copy the code in > https://ramdajs.com/repl/?v=0.26.1 and everything ok but in nodejs I do the > var R = require ('ramda'); Any suggestions? – Denis Alayza Dec 04 '19 at 02:20
  • I'm sorry, @DenisAlayza, I'm not sure what you're asking. This works fine in the Ramda REPL. (That's where I developed it.) And it works in a node environment, for instance in https://repl.it/@CrossEye/SO-question-59161831. – Scott Sauyet Dec 04 '19 at 03:07
  • Thanks Scott, don't explain to me well, if the result changes as I edited it just now, the result is empty. – Denis Alayza Dec 04 '19 at 03:41
  • @DenisAlayza: But you changed the name of the property that's being compared. You need to change the code to match, that is, change `safeId` to `seguroAfiliado`. See the updates in the answer above. – Scott Sauyet Dec 04 '19 at 14:50
  • Please, if you can see the result you throw at me, edit the question and paste a link. – Denis Alayza Dec 05 '19 at 01:44
  • @DenisAlayza: I'm afraid I'm not entirely following your English. But I *did* edit this answer and pasted a link. – Scott Sauyet Dec 05 '19 at 02:04
  • I copy your code and the result emitted by nodejs is attached in the ramda link. Wrong result in nodejs. – Denis Alayza Dec 05 '19 at 13:16
  • @DenisAlayza: It seems to work fine for me in Node. See the updated answer. I think you have something additional going on. – Scott Sauyet Dec 05 '19 at 14:14
  • I can give you temporary access to my code in the repository... i need your email... – Denis Alayza Dec 06 '19 at 02:16
  • I think you need to ask a separate question. But first read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Trying to do that often helps you figure out the problem yourself. But if not, then it makes it easier for others to help you. – Scott Sauyet Dec 06 '19 at 02:43
  • What I need is just what I ask and it works in ramda and any test site in nodejs, but in my code it throws me is a json with attributes that don't show the requested information... It urges me since I have days with this problem and I need to move forward ... – Denis Alayza Dec 06 '19 at 03:36
  • @DenisAlayza: What that means is that what you're running is not what your question is asking. That's why it's important to be able to demonstrate your issue with a minimal, complete, and verifiable example. StackOverflow is about asking and answering technical questions. You need to put the work into your question if you want people to put work into their answers. Your urgency does not motivate those trying to help here. A well-asked question does. – Scott Sauyet Dec 06 '19 at 13:38
  • Attach print screen of the results – Denis Alayza Dec 06 '19 at 14:15
  • @DenisAlayza: That's not likely to be helpful at all. Please read that link I suggested four comments up. Then, if that process hasn't helped you solve it yourself, ask a new question that includes such an example. – Scott Sauyet Dec 06 '19 at 14:20
  • I no longer know how to get the solution at this point ... I send all the console.log, postman results, program code, I give access to them and still I am in it. I did console.log of everything and in the enhance variable the rest field is empty ... I copy the same code in try ramda and it works ... – Denis Alayza Dec 06 '19 at 15:03
  • Again, I think you're missing the point of StackOverflow. Please see https://stackoverflow.com/help/how-to-ask. – Scott Sauyet Dec 06 '19 at 15:30