how to create new array if condition does not satisfied? - javascript

I am trying to create array for ids that are part of rxInfo but are not matching with members ids, but its always push memberIds to mismatchIndexIDs.how to check that condition if value is thereand not matching push it to array.
there could be case i will have 4 members in specialMembers and rxInfos only has 2 passed.
main.ts
for(const member of specialMembers) {
for (const rxInfo of this.rxInfos) {
if (member.indexID === rxInfo.indexID) {
this.indexIDs.push(rxInfo.indexID);
proxyMember = member;
if (!member.dateOfBirth) {
statusDesc = "member dateOfbirth not found";
return Promise.reject(this.errorHandler(request, statusDesc));
}
const requestBody: any = this.buildSingleRequestBody(proxyMember, rxInfo);
const requestObject = this.specialtyQuestionRequest(requestBody);
this.requestArray.push(requestObject);
} else {
this.mismatchIndexIDS.push(rxInfo.indexID);
this.indexIdMismatchCounter++;
}
}
}
data:
"rxInfos": [
{
"drugNdc": "10101",
"rxNumber": "14556459709",
"firstFillIndicator": "N",
"sourceSystem": "TBS",
"indexID": "RPT0ifQ"
},
{
"drugNdc": "101",
"rxNumber": "145945000709",
"firstFillIndicator": "N",
"sourceSystem": "TBS",
"indexID": "GJhQ1MrQnZkTFRR"
}
]
"specialyMembers":[
{
"dob":"12-12-1970"
"firstName": "jimmy",
"lasteName": "shew",
"indexID": "RPT0ifQ"
},
{
"dob":"18-10-1970"
"firstName": "Timmy",
"lasteName": "Doug",
"indexID": "GJhQ1MrQ"
},
{
"dob":"17-06-1981"
"firstName": "John",
"lasteName": "owascar",
"indexID": "GJhQ1MrTGDSRQ"
}
]

Instead of looping over two arrays (O(N^2) operations), transform the first one in a temporary object indexed by the joining key and partition the second array.
function partition(arr, predicate) {
const out = [[],[]];
arr.forEach(e => out[Number(!!predicate(e))].push(e));
return out;
}
const membersByIndex = {}
specialMembers.forEach(m => membersByIndex[m.indexID] = m)
const [mismatch, match] = partition(rxInfo, rx => rx.indexID in membersByIndex)

Related

moving a key value pair out of an array

I am trying to move everything in the Array Results outside and into the original object
this is the object
{
"Name": "John",
"Results": [
{
"Type": "DB",
"Immediate_Action": "No",
}
]
}
It should look like this
{
"Name": "John",
"Type": "DB",
"Immediate_Action": "No",
}
What I have so far is this
const mapOscarResults = ({ data }) => {
return data.map(entry => {
let mapped = {...entry};
entry.Results.forEach(key => {
let Type = mapped[key.Type]
if (mapped[key]) {
mapped[key].push(entry.Results[key]);
} else {
mapped[key] = [entry.Results[key]];
}
});
return mapped;
});
};
You can simply spread the Results array into an Object.assign() call.
const input = { "Name": "John", "Results": [{ "Type": "DB", "Immediate_Action": "No", }, { "Another": "value" }] };
const { Results, ...refactored } = input;
Object.assign(refactored, ...Results);
console.log(refactored)
This code works for your example:
const { Results: results, ...rest } = {
"Name": "John",
"Results": [
{
"Type": "DB",
"Immediate_Action": "No",
}
]
}
const res = {...rest, ...results.reduce((prev, curr) => ({
...prev,
...curr
}), {})}
console.log(res)
But I don't know what you expect when the Results array has more than one element.
In that condition, if this code does not fill your needs, ask me to change it.
however, it will join first Result with index 0, you can expand it
const data = {
"Name": "John",
"Results": [
{
"Type": "DB",
"Immediate_Action": "No",
}
]
}
const mapOscarResults = (data) => {
for (let i in Object.keys(data)){
if (Array.isArray(data[Object.keys(data)[i]])){
newKey = data[Object.keys(data)[i]][0]
data = {... data, ...newKey}
delete data[Object.keys(data)[i]]
}
}
return data
};
console.log(mapOscarResults(data))

filtering list by multiple conditions

there is a list of users
filterData = [
{
"position":"lawyer",
"department_positions":[],
"group_positions":[
{"group":{"id":2,"code":"234","name":"group1"},"lead":false},
{"group":{"id":1,"code":"123","name":"group12"},"lead":true}
]
},
{
"position":"director",
"department_positions":[
{"department":{"id":3,"code":"333","name":"subDep"},"lead":false}
],
"group_positions":[
{"group":{"id":2,"code":"234","name":"group1"},"lead":false},
{"group":{"id":1,"code":"123","name":"group12"},"lead":true}
]
},
{
"position":"director",
"department_positions":[],
"group_positions":[]
}
]
and list of filters
categories = {
"position":["lawyer","director"],
"group_positions":["group1","group12"],
"department_positions":["generalDep", "subDep"]
}
It is necessary to filter users taking into account the fact that several filters can be selected at the same time. For example, i want to find user with position = "director" and AND group_positions = "group1" AND department_positions = "subDep"
my code doesn't allow filtering by multiple conditions. how can i fix it?
this.filter = this.filterData.filter(item => {
for (let key in this.categories) {
if (item[key].find(el =>
this.categories[key].includes(
el.group?.name || el.department?.name
)
)) {
return true
}
}
return false
})}
This is a good place to employ an es6 class to give behavior to the object being filtered. Augment each object to determine if it matches the "category" object.
(from the example data, this assumes the OP is looking for a "product of sums" match: for all of the category keys match at least one of the category values)
class FilterMe {
constructor(item) {
Object.assign(this, item);
}
namesForKey(key) {
switch (key) {
case 'position':
return [this.position]; // always answer an array
case 'group_positions':
return this.group_positions.map(gp => gp.group.name);
case 'department_positions':
return this.department_positions.map(dp => dp.department.name);
default:
return [];
}
}
// return true if a single filter key-value pair is matched
matchesFilterKeyValue(filterKey, filterOptions) {
const myNames = this.namesForKey(filterKey);
const matches = filterOptions.filter(e => myNames.includes(e));
return matches.length > 0;
}
// return true if all filter key-values pairs are matched
matchesFilter(filter) {
return Object.entries(filter).every(keyValue => {
return this.matchesFilterKeyValue(...keyValue);
})
}
}
const filterData = [{
"position": "lawyer",
"department_positions": [],
"group_positions": [{
"group": {
"id": 2,
"code": "234",
"name": "group1"
},
"lead": false
}, {
"group": {
"id": 1,
"code": "123",
"name": "group12"
},
"lead": true
}]
},
{
"position": "director",
"department_positions": [{
"department": {
"id": 3,
"code": "333",
"name": "subDep"
},
"lead": false
}],
"group_positions": [{
"group": {
"id": 2,
"code": "234",
"name": "group1"
},
"lead": false
}, {
"group": {
"id": 1,
"code": "123",
"name": "group12"
},
"lead": true
}]
},
{
"position": "director",
"department_positions": [],
"group_positions": []
}
]
const categories = {
"position": ["lawyer", "director"],
"group_positions": ["group1", "group12"],
"department_positions": ["generalDep", "subDep"]
}
// convert the filterData to the objects and test them...
let objects = filterData.map(d => new FilterMe(d));
let matches = objects.filter(o => o.matchesFilter(categories))
console.log(matches)
You can try something like this:
let filtered = example.filter(item => {
let valid = false
if (item.includes('something')) {
valid = true
}
if (!valid) {
// check second condition
}
return valid
})
Use a temporary placeholder so you don't immediately have to return true/false.

merge duplicate objects in an array and combine sub array of each object

I am trying to merge objects based off of Id, and merge each array that lives inside each account (object), but instead of merging the contents of accountList, the code overwrites the array, if there is a matching id.
I've made a new array and used the .find method to find matching objects based off there id, but stuck on how to merge the accountList together
const accounts = [
{
"Id": 103,
"accountList": [
{}
]
},
{
"Id": 103,
"accountList": [
{
"tokenId": "5aasdasdsdnjn3434nadd",
"featureId": 2840
}
]
},
{
"Id": 112,
"accountList": [
{
"tokenId": "5d30775bef4a722c38aefaaa",
"featureId": 2877
}
]
},
{
"Id": 112,
"accountList": [
{
"tokenId": "5d30775bef4a722c38aefccc",
"featureId": 2856
}
]
}
]
let result = [];
accounts.forEach(account => {
let match = result.find(r => r.Id === account.Id);
// console.log(match)
if(match) {
Object.assign(match, account);
//tried using spread operator instead of object assign, but didnt work
// match = {...match, ...account}
} else {
result.push(account);
}
});
console.log( JSON.stringify(result, null, 2))
The result which i need is to merge the object based off their id, and merge the contents of the accountList together, like so:
[
{
"Id": 103,
"accountList": [
{
"tokenId": "5aasdasdsdnjn3434nadd",
"featureId": 2840
}
]
},
{
"Id": 112,
"accountList": [
{
"tokenId": "5d30775bef4a722c38aefaaa",
"featureId": 2877
},
{
"tokenId": "5d30775bef4a722c38aefccc",
"featureId": 2856
}
]
}
]
I think, reduce() would do the job:
const accounts = [{"Id":103,"accountList":[{}]},{"Id":103,"accountList":[{"tokenId":"5aasdasdsdnjn3434nadd","featureId":2840}]},{"Id":112,"accountList":[{"tokenId":"5d30775bef4a722c38aefaaa","featureId":2877}]},{"Id":112,"accountList":[{"tokenId":"5d30775bef4a722c38aefccc","featureId":2856}]}];
const result = [...accounts
.reduce((r, o) => {
const record = r.get(o.Id)||{}
r.set(o.Id, {
Id: o.Id,
accountList: [
...(record.accountList||[]),
...o.accountList.filter(o =>
Object.keys(o).length != 0)
]
})
return r
}, new Map())
.values()]
console.log(result);
.as-console-wrapper {min-height: 100%}
You can try to use Array.concat:
let result = [];
accounts.forEach(account => {
let match = result.find(r => r.Id === account.Id);
// console.log(match)
if(match) {
match.accountList = match.accountList.concat(account.accountList);
} else {
result.push(account);
}
});
for (let res of result) {
console.log('res.Id: ', res.Id, res.accountList)
}
// res.Id: 103 [ {}, { tokenId: '5aasdasdsdnjn3434nadd', featureId: 2840 } ]
// res.Id: 112 [ { tokenId: '5d30775bef4a722c38aefaaa', featureId: 2877 },
// { tokenId: '5d30775bef4a722c38aefccc', featureId: 2856 } ]
Using Array.prototype.reduce we can accumulate the results in the final result array.
In the reduce call back just find the matching object using Id and merge the accountList array and not the object as you were doing in your code.
const accounts=[{"Id":103,"accountList":[{}]},{"Id":103,"accountList":[{"tokenId":"5aasdasdsdnjn3434nadd","featureId":2840}]},{"Id":112,"accountList":[{"tokenId":"5d30775bef4a722c38aefaaa","featureId":2877}]},{"Id":112,"accountList":[{"tokenId":"5d30775bef4a722c38aefccc","featureId":2856}]}];
const result = accounts.reduce((acc, account) => {
let match = acc.find(r => r.Id === account.Id);
if(match) {
match.accountList.push(...account.accountList); //push previous array
} else {
const act = { ...account };
act.accountList = account.accountList.filter((obj) => Object.keys(obj).length);
acc.push(act);
}
return acc;
}, []);
console.log(result);
I think you could use match.accountList.push(...account.accountList); instead of the object assign, spread operator can be used to push the element into the result item(match):
let accounts = [{ "Id": 103, "accountList": [{}] }, { "Id": 103, "accountList": [{ "tokenId": "5aasdasdsdnjn3434nadd", "featureId": 2840 }] }, { "Id": 112, "accountList": [{ "tokenId": "5d30775bef4a722c38aefaaa", "featureId": 2877 }] }, { "Id": 112, "accountList": [{ "tokenId": "5d30775bef4a722c38aefccc", "featureId": 2856 }] }];
let result = [];
accounts.forEach(account => {
(match = result.find(r => r.Id === account.Id), match ? match.accountList.push(...account.accountList) : result.push(account))
});
console.log(result);
const isNotEmptyObject = objc => Object.entries(objc).length > 0;
function mergeAccounts(accounts) {
const uniqueAccounts = new Map();
accounts.forEach(account => {
if(uniqueAccounts.has(account.Id)) {
let uniqueAccount = uniqueAccounts.get(account.Id);
if(account.accountList && account.accountList.length > 0)
uniqueAccount.accountList.push(...account.accountList);
uniqueAccount.accountList = uniqueAccount.accountList.filter(isNotEmptyObject);
} else {
uniqueAccounts.set(account.Id, account);
}
});
return Array.from(uniqueAccounts.values());
}
This will merge all the accounts having same ids. Hope this helps :)

How to sort an object of objects based on the keys in descending order?

I'm trying to order an array of object of objects based on the object's key.
How do I go about sorting a JSON Object that's already in ascending order? Please see below.
I've tried to convert the data value object into its own array, then sort it that way, but I'm getting syntax errors.
var object = [
A1: {
errors: {}
otherData: {}
data: {
"1": {
"name": "Ashley",
},
"2": {
"name": "Cardiff",
},
"3": {
"name": "Reading",
}
}},
A2: {
errors: {}
otherData: {}
data: {
"4": {
"name": "Susan",
},
"5": {
"name": "Bee",
},
"6": {
"name": "Bob",
}
}}];
I want it to be:
var object = [
A1: {
errors: {}
otherData: {}
data: {
"3": {
"name": "Reading",
},
"2": {
"name": "Cardiff",
},
"1": {
"name": "Ashley",
}
}},
A2: {
errors: {}
otherData: {}
data: {
"6": {
"name": "Bob",
},
"5": {
"name": "Bee",
},
"4": {
"name": "Susan",
}
}}];
If I understand correctly you want to sort the in alphabetical order by first letter. This is kind of out there but it should do what you're looking for
const arr1 = object.map(function(o) {
return Object.values(o[Object.keys(o)].data).sort((a, b) => a - b);
})
I'll try to explain what's happening here. map is iterating over each object in the array and return a brand new array. o[Object.keys(o)].data is selecting A1 and A2 data keys. Then we're saying we want the values of the objects in those data objects with the surrounds Object.keys() which is giving us the names. From there we're just calling sort and giving it the callback.
const sortByField = (field, isRevered = false, primerFn) => {
if (field) {
var key = primerFn ? (x) => primerFn(x[field]) : (x) => x[field];
isRevered = !isRevered ? 1 : -1;
return (a, b) => {
/*eslint-disable */
return a = key(a), b = key(b), isRevered * ((a > b) - (b > a));
/*eslint-enable */
}
}
else {
return (a, b) => {
return isRevered ? a < b : a > b;
}
}
}
var dataToSort = {
A1: {
errors: {},
otherData: {},
data: {
"1": { "name": "Ashley", },
"2": { "name": "Cardiff", },
"3": { "name": "Reading", }
}
},
A2: {
errors: {},
otherData: {},
data: {
"4": { "name": "Susan", },
"5": { "name": "Bee", },
"6": { "name": "Bob", }
}
}
};
const sortObjectByKeys = (obj) => {
let values = [];
let keys = [];
Object.keys(obj).forEach(key => {
keys.push(key);
values.push(obj[key])
})
values.sort(sortByField("name", true, (value) => value.toLowerCase()));
let sortedObject = {};
values.forEach((value, index) => {
sortedObject[keys[index]] = value;
})
return sortedObject;
}
let sortedData = {};
Object.keys(dataToSort).forEach(dataKey => {
sortedData[dataKey] = {
...dataToSort[dataKey],
data: sortObjectByKeys(dataToSort[dataKey].data)
}
})
console.log(JSON.stringify(sortedData));

Build array from another array if some key are identical using JavaScript

I have an array of data. Some of the key in the array are same. I would like to create a new array based on the key and add the other data.
This is my array
var myObjOne = [
{
"name":"John",
"id":1,
"car":"maruti"
},
{
"name":"John",
"id":2,
"car":"wolks"
},
{
"name":"John",
"id":3,
"car":"bmw"
},
{
"name":"Peter",
"id":4,
"car":"alto"
},
{
"name":"Peter",
"id":5,
"car":"swift"
}
];
I would like to convert the array in to the below format.
var myObj = [
{
"name":"John",
"items": [
{ "id":1, "car":"maruti" },
{ "id":2, "car":"wolks" },
{ "id":3, "car":"bmw" }
]},
{
"name":"Peter",
"items": [
{ "id":4, "car":"alto" },
{ "id":5, "car":"swift" },
]
}
];
I am working on a node environment.
You can create an object using Array#reduce first which maps name with items, and then create the final array by looping over the intermediate map using a for...of loop:
var source = [{"name":"John","id":1,"car":"maruti"},{"name":"John","id":2,"car":"wolks"},{"name":"John","id":3,"car":"bmw"},{"name":"Peter","id":4,"cars":"alto"},{"name":"Peter","id":5,"cars":"swift"}];
const map = source.reduce((acc, {name, ...obj}) => {
if (!acc[name]) {
acc[name] = [];
}
acc[name].push(obj);
return acc;
}, {});
const result = [];
for (let[name, items] of Object.entries(map)) {
result.push({name, items});
}
console.log(result);
Array.reduce is at rescue.This method accepts an accumulator and current
item. Check in the accumulator if there exist an object where the value of name property is John or Peter
var myObjOne = [{
"name": "John",
"id": 1,
"car": "maruti"
},
{
"name": "John",
"id": 2,
"car": "wolks"
},
{
"name": "John",
"id": 3,
"car": "bmw"
},
{
"name": "Peter",
"id": 4,
"car": "alto"
},
{
"name": "Peter",
"id": 5,
"car": "swift"
}
];
var newObj = myObjOne.reduce(function(acc, curr, currIndex) {
// using findIndex to check if there exist an object
// where the value of the name property is John, Peter
// if it exist it will return the index else it will return -1
let ifNameExist = acc.findIndex(function(item) {
return item.name === curr.name;
})
// if -1 then create a object with name and item property and push
// it to the accumulator
if (ifNameExist === -1) {
let nameObj = {};
nameObj.name = curr.name;
nameObj.items = [];
nameObj.items.push({
id: curr.id,
car: curr.car
})
acc.push(nameObj)
} else {
// if such an object already exist then just update the item array
acc[ifNameExist].items.push({
id: curr.id,
car: curr.car
})
}
return acc;
}, []);
console.log(newObj)
Use .reduce to group by name, and use .find inside the reducer to find if the matching name has already been added:
const input=[{"name":"John","id":1,"car":"maruti"},{"name":"John","id":2,"car":"wolks"},{"name":"John","id":3,"car":"bmw"},{"name":"Peter","id":4,"cars":"alto"},{"name":"Peter","id":5,"cars":"swift"}]
const output = input.reduce((a, { name, ...item }) => {
const foundNameObj = a.find(nameObj => nameObj.name === name);
if (foundNameObj) foundNameObj.items.push(item);
else a.push({ name, items: [item] });
return a;
}, []);
console.log(output);

Categories