How to turn this ID-based objects into an array of objects? - javascript

I have data like this:
{
"-L8BpxbS70KYrZMQUF0W": {
"createdAt": "2018-03-22T16:33:57+08:00",
"email": "ss#ss.ss",
"name": "ss"
},
"-KYrZMQUF0WL8BpxbS70": {
// etc.
}
}
Which I want to turn into this:
[{
id: '-L8BpxbS70KYrZMQUF0W
createdAt: "2018-03-22T16:33:57+08:00",
email: "ss#ss.ss",
name: "ss"
}, {
id: -KYrZMQUF0WL8BpxbS70"
// etc.
}]
I'm started with this:
Object.keys(idBasedObjects).forEach(key => {
console.log(resp[key])
})
But I get undefined.
What's best way of creating this array?

Get the keys and values using Object.entries(), and Array.map() them to to the required form using object spread:
const obj = {"-L8BpxbS70KYrZMQUF0W":{"createdAt":"2018-03-22T16:33:57+08:00","email":"ss#ss.ss","name":"ss"},"-KYrZMQUF0WL8BpxbS70":{}};
const result = Object.entries(obj).map(([id, props]) => ({
id,
...props
}));
console.log(result);

Use Object.keys, Object.assign and map
var output = Object.keys(obj).map( s => Object.assign( obj[s], {id : s} ))
Demo
var obj = {
"-L8BpxbS70KYrZMQUF0W": {
"createdAt": "2018-03-22T16:33:57+08:00",
"email": "ss#ss.ss",
"name": "ss"
},
"-KYrZMQUF0WL8BpxbS70": {
"createdAt": "2018-03-22T16:33:57+08:00",
"email": "s2s#ss.ss",
"name": "ss2"
}
};
var output = Object.keys(obj).map(s => Object.assign(obj[s], {
id: s
}));
console.log(output);

Related

If object set has an object property create an array

I have a response value which is dynamic which i need to store in redux state,
Response consist of array of object and and name
ex :
{data:[
{name:"abc",age:"10",id:"10"}
{name:"abc",age:"15",id:"20"}
{name:"def",age:"15",id:"20"}
]
name: "abc"
}
So if the name is same I need to create array with the name.
Expected :
abc:[
{name:"abc",age:"10",id:"10"}
{name:"abc",age:"15",id:"20"}
]
something I tried
data.map(function(o) {
if(data.name ==o.name)
return name[o];
});
If you're wanting a new object with a key of the name property you could try something like this
const response = {
data: [{
name: "abc",
age: "10",
id: "10"
},
{
name: "abc",
age: "15",
id: "20"
},
{
name: "def",
age: "15",
id: "20"
},
],
name: "abc"
}
const createSet = (someData) => {
let key = someData.name
let data = someData.data.filter(e => e.name === key)
return {
[key]: data
}
}
console.log(createSet(response))
You can extract duplicated using reduce and filter :
var data = {
data:[
{name:"abc",age:"10",id:"10"},
{name:"abc",age:"15",id:"20"},
{name:"def",age:"15",id:"20"}
],
name: "abc"
}
const lookup = data.data.reduce((a, e) => {
a[e.name] = ++a[e.name] || 0;
return a;
}, {});
console.log(data.data.filter(e => lookup[e.name]));

How to filter array of objects with nested objects with specific properties?

I am trying to filter a Javascript array of objects with nested objects with specific properties. I can filter the name, slug, website, launch year without any issues. But, I can not filter the category name (category.name) which is an object within the object. Why is filtering the category name not working?
var search = "qui"; // does not work (category.name)
// var search = "Sauer"; // works (name)
var data = [{ "name": "Sauer-Metz", "slug": "ab-laborum",
"website": "https://test.com", "launch_year": 2017, "category_id": 6,
"category": { "id": 6, "name": "qui", "slug": "qui" } } ];
var results = data.filter(company => [
'name', 'launch_year', 'website', 'category.name'
].some(key => String(company[key]).toLowerCase().includes(search.toLowerCase())));
console.log(results);
One way you can go about it is to have a value extractor like the one getKey below
const getKey = (value, key) => {
return key.split('.').reduce((acc, curr) => value[curr], '');
}
var results = data.filter(company => [
'name', 'launch_year', 'website', 'category.name'
].some(key => String(getKey(company, key)).toLowerCase().includes(search.toLowerCase())));
I believe you have to do a separate condition for this specific nested property, although there might be a cleaner way I don't see right now:
var results = data.filter(
(company) =>
["name", "launch_year", "website"].some((key) =>
String(company[key]).toLowerCase().includes(search.toLowerCase())
) ||
String(company["category"]["name"])
.toLowerCase()
.includes(search.toLowerCase())
);
Dot notation doesn't work like that.
const testCase1 = 'qui';
const testCase2 = 'Sauer';
const data = [
{
name: 'Sauer-Metz',
slug: 'ab-laborum',
website: 'https://test.com',
launch_year: 2017,
category_id: 6,
category: { id: 6, name: 'qui', slug: 'qui' },
},
];
const searchResults = (data, search) => {
return data.filter((item) => {
return (
item?.category?.name.toLowerCase().includes(search.toLowerCase()) ||
['name', 'launch_year', 'website'].some((key) => `${item[key]}`.toLowerCase().includes(search.toLowerCase()))
);
});
};
console.log('**CASE 1**')
console.log(searchResults(data, testCase1));
console.log('**CASE 2**')
console.log(searchResults(data, testCase2));
To use your approach you can convert 'category.name' to ['category','name'] and then use String(company[key[0]][key[1]])... whenever key is an array.
const search = "qui"; // does not work (category.name)
//const search = "Sauer"; // works (name)
const data = [{ "name": "Sauer-Metz", "slug": "ab-laborum", "website": "https://test.com", "launch_year": 2017, "category_id": 6, "category": { "id": 6, "name": "qui", "slug": "qui" } } ];
const results = data.filter(
company => [
'name', 'launch_year', 'website', ['category','name']
].some(
key =>
Array.isArray(key) ?
String(company[key[0]][key[1]]).toLowerCase().includes(search.toLowerCase()) :
String(company[key]).toLowerCase().includes(search.toLowerCase())
)
);
console.log(results);

Convert Object which has value an Array to another Array of Object

I have this kind of object:
{
"John":[
{
"address":"xxx1",
"city":"yyy1"
},
{
"address":"xxx2",
"city":"yyy2"
}
],
"Doe":[
{
"address":"aaaa1",
"city":"aaa1"
}
],
"Smith":[
{
"address":"bbb1",
"city":"bbb1"
}
],
}
What I try to achieve is to reduce this object so it look like this:
[
{
"name":"John",
"address":"xxx1",
"city":"yyy1"
},
{
"name":"John",
"address":"xxx2",
"city":"yyy2"
},
{
"name":"Doe",
"address":"aaaa1",
"city":"aaaa1"
},
{
"name":"Smith",
"address":"bbb1",
"city":"bbb1"
}
]
But I'm sure that the same thing can be done somehow by using the ES6 array.reduce. Can you help me? I looked at JS (ES6): Reduce array based on object attribute but I can't figure it out.
const modifiedData = Object.entries(data).reduce(function (acc, [key,value]) {
const personName = key;
return [
...acc,
{
Agent: personName ,
adress: value.adress
},
];
}, []);
You can achieve this using reduce.
const obj = {
John: [
{
address: "xxx1",
city: "yyy1",
},
{
address: "xxx2",
city: "yyy2",
},
],
Doe: [
{
address: "aaaa1",
city: "aaa1",
},
],
Smith: [
{
address: "bbb1",
city: "bbb1",
},
],
};
const result = Object.entries(obj).reduce((acc, [key, arr]) => {
const collection = arr.map((a) => ({ name: key, ...a }));
acc = [...acc, ...collection];
return acc;
}, []);
console.log( result );
The simple way like this.
const data = {"John":[{"address":"xxx1","city":"yyy1"},{"address":"xxx2","city":"yyy2"}],"Doe":[{"address":"aaaa1","city":"aaa1"}],"Smith":[{"address":"bbb1","city":"bbb1"}],};;
const result = Object.entries(data).flatMap(([key, values]) =>
values.map(o => ({name: key, ...o})));
console.log(result);
If you want to do it using Array.prototype.reduce, you can do something like this:
const input = {
"John": [{
"address": "xxx1",
"city": "yyy1"
},
{
"address": "xxx2",
"city": "yyy2"
}
],
"Doe": [{
"address": "aaaa1",
"city": "aaa1"
}
],
"Smith": [{
"address": "bbb1",
"city": "bbb1"
}],
}
// 1. Using Object.keys()
const output1 = Object.keys(input).reduce((acc, person) => {
input[person].forEach(item => {
acc.push({ name: person, ...item })
})
return acc;
}, []);
console.log('output1:', output1)
// 2. Using Object.entries()
const output2 = Object.entries(input).reduce((acc, [key, value]) => {
value.forEach(item => {
acc.push({ name: key, ...item })
});
return acc;
}, [])
console.log('output2:', output2);

How to read an object from an array that has only 0 index in javascript

[
{
"first" : {
"email" : "abofahad.en#gmail.com"
},
"second" : {
"email" : "aaa#gmail.com"
}
}
]
as you know first and second is inside index 0 but how can read them using map without using like
user.map(c=>c.first.email)
let users = [
{email: "abofahad.en#gmail.com"},
{email: "aaa#gmail.com"}
]
// .map() return new array
let user = users.map((item) => item.email)
console.log(user)
console.log(user[0])
console.log(user[1])
It's no less clunky, but Object.keys() will achieve the same results
let obj = {
"user": {
"first": {
"email": "abofahad.en#gmail.com"
},
"second": {
"email": "aaa#gmail.com"
}
}
}
let res = Object.keys(obj.user).map(el => {
return obj.user[el].email
})
console.log(res)

Item search between two array in nodejs

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:
Code modified with values 'safes' and 'citasMedicas':
Result with POSTMAN:
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;
}, []))
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)
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.
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>
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);
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"
}
]

Categories

Resources