I am looking for a function that can mutate my data i.e array of object with a nested object. It will include keys that have object/array values (it should only include keys with immediate string/number/boolean values).
Example
[
{
id: 1,
person1: {
firstname: "test1",
lastname: 'singh',
address: {
state: "maharashtra",
}
}
},
{
id: 2,
person2: {
firstname: "test2",
lastname: 'rathod',
address: {
state: "kerala",
}
}
},
{
id: 3,
person3: {
firstname: "test3",
lastname: 'gokale',
address: {
state: "Tamilnadu",
}
}
}
]
Expected output
[
{
title: 'person1',
value: 'person.id'
},
{
title: 'person1',
value: 'person.firstname'
},
{
title: 'person1',
value: 'person.lastname'
},
{
title: 'person1',
value: 'person.address'
},
{
title: 'person1',
value: 'person.address.state'
},
...sameforOthers
]
Basically, I need a function that will get an array and will return an array of objects as a given above as expected output
Thanks in advance
I have come up with a solution. below is the link for code sandbox for the
https://codesandbox.io/s/heuristic-rubin-yy2cyy?file=/src/index.js:0-213same
const suggestions = [
{
id: 1,
person1: {
id: "1",
firstname: "test1",
lastname: "singh",
address: {
state: "maharashtra"
},
attributeId: "fhgfgh"
}
}
];
const typeList = ["string", "number", "boolean"];
const getLabelValue = (itemList, initalArr, parentId) => {
if (Array.isArray(itemList)) {
itemList.forEach((currentItem, idx) => {
const id = parentId ? `${parentId}.${idx}` : idx;
if (typeList.includes(typeof currentItem)) {
initalArr.push({
title: id,
value: id
});
} else {
getLabelValue(currentItem, initalArr, id);
}
});
} else {
let keys = Object.keys(itemList);
keys.forEach((currentKey) => {
let currentItem = itemList[currentKey];
const id = parentId ? `${parentId}.${currentKey}` : currentKey;
if (typeList.includes(typeof currentItem)) {
initalArr.push({
title: id,
value: id
});
} else {
getLabelValue(currentItem, initalArr, id);
}
});
}
return initalArr;
};
console.log(">>>>>>>>>", getLabelValue(suggestions, [], ""));
Related
var employees = [
{ name: "Josh", title: "receptionist" },
{ name: "Naila", title: "receptionist" },
{ name: "Tom", title: "doctor" },
{ name: "Becky", title: "doctor" }
];
For example on this one I would like to return
{
'doctor':2,
'receptionist':2
}
This is what I have tried:
const convert = (employees) => {
const res = {};
employees.forEach((employee) => {
const key = `${employee.title}${employee["doctor-receptionist"]}`;
if (!res[key]) {
res[key] = {...employee, count: 0 };
};
res[key].count += 1;
});
return Object.values(res);
};
console.log(convert(employees));
It returns the name of the employees, which I did not want.
I also thought about creating arrays for each kind of job title and filtering each employee from the employee array, and pushing them to their respective arrays. But I feel like there must be an easier way.
Array#reduce is the way to go:
const employees = [ { name: "Josh", title: "receptionist" }, { name: "Naila", title: "receptionist" }, { name: "Tom", title: "doctor" }, { name: "Becky", title: "doctor" } ],
summary = employees
.reduce((acc,{title}) => ({...acc,[title]:(acc[title] || 0) + 1}),{});
console.log( summary );
Just using reduce() can do it
var employees = [
{ name: "Josh", title: "receptionist" },
{ name: "Naila", title: "receptionist" },
{ name: "Tom", title: "doctor" },
{ name: "Becky", title: "doctor" }
];
let result = employees.reduce((a,c) =>{
a[c.title] = a[c.title] ? a[c.title] + 1 : 1
return a
},{})
console.log(result)
The reduce iterator was built for this kind of thing.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
I also employ Object.values() since the way I am using reduce is to create an object to easily keep track of the data along the way. The object.values helps distill that into an array when done.
const employees = [
{ name: "Josh", title: "receptionist" },
{ name: "Naila", title: "receptionist" },
{ name: "Tom", title: "doctor" },
{ name: "Becky", title: "doctor" }];
const reduced = Object.values(employees.reduce((b,a) => {
if (!b[a.title]) b[a.title] = {title: a.title, count: 1}
else b[a.title].count++;
return b
},{}))
console.log(reduced);
you can try this on your code
const employees = [
{ name: "Josh", title: "receptionist" },
{ name: "Naila", title: "receptionist" },
{ name: "Tom", title: "doctor" },
{ name: "Becky", title: "doctor" }
]
const sumReceptionist = employees.filter((item)=>{
return item.title === 'receptionist'
}).length
const sumDoctor = employees.filter((item)=>{
return item.title === 'doctor'
}).length
let total =
{
receptionist: sumReceptionist,
doctor: sumDoctor
}
console.log(total)
I think this is what you're trying to do. You want the total of the positions from the employee list?
const Employees = [{
name: "Josh",
title: "receptionist"
},
{
name: "Naila",
title: "receptionist"
},
{
name: "Tom",
title: "doctor"
},
{
name: "Becky",
title: "doctor"
},
{
name: "Chad",
title: "doctor"
},
{
name: "Cindy",
title: "nurse"
}
];
// A forEach won't return an object or array, so we create one to modify within it
const PositionTotals = {};
Employees.forEach(employee => {
// Check if property exists. If not, create it and add one to it before continuing loop
if (!PositionTotals.hasOwnProperty(employee.title))
return PositionTotals[employee.title] = 1;
PositionTotals[employee.title]++;
})
console.log(PositionTotals);
$('#PositionTotals').html(JSON.stringify(PositionTotals, null, '\t'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<pre id="PositionTotals"></pre>
i am trying to find a list of synced users between two arrays (objArray1 & objArray2) and return the data from objArray1 along with 'aid' in Objarray2. I have the below code working, but i am trying to see if can return 'aid' from objArray2 as well in the below format.
Code sample below
// SystemA
const objArray1 = [
{ id: "1", name: "John" },
{ id: "2", name: "Jack" },
{ id: "3", name: "Sam" },
{ id: "4", name: "Bill" },
];
// SystemB
const objArray2 = [
{ id: "1", name: "John", aid:"uuud2905555" },
{ id: "3", name: "Sam" }, aid:"uuud29029303"
{ id: "5", name: "Bob" }, aid:"uuud29435454"
];
const array1IDs = objArray1.map((item) => {
return item.id
})
// Result: array1IDs = ["1", "2", "3", "4"];
const array2IDs = objArray2.map((item) => {
return item.id
})
// Result: array2IDs = ["1", "3", "5"];
// FIND SYNCED USERS
// Compare the id value of each item in objArray1 with each item of objArray2
// Return the ones that match.
const syncedUsers = objArray1.filter((item) => {
const found = objArray2.find((element) => element.id === item.id);
return found;
});
Required JSON Format, please note that all matching items should be returned from objArray1, with the exception of 'aid' from objArray2
{
"records": [
{
"id": {aid}, // from objArray2
"fields": {
"Name":{name}, // from objArray1
"sid": {id} // from objArray1
}
]
}
Presented below is one possible way to achieve the desired objective.
Code Snippet
// method to create result as "JSON"
const createJSON = (arr1, arr2) => (
// use "stringify" to transform JavaScript object into JSON
JSON.stringify({
// set-up the "records" prop
records: arr2
.filter( // filter to keep only those that have matching "id"
({ id }) => arr1.some(ob => ob.id === id)
)
.map( // de-structure & rename props to match desired objective
({ id : sid, name : Name, aid: id }) => ({
id,
fields: {Name, sid}
})
)
})
);
// SystemA
const objArray1 = [
{ id: "1", name: "John" },
{ id: "2", name: "Jack" },
{ id: "3", name: "Sam" },
{ id: "4", name: "Bill" },
];
// SystemB
const objArray2 = [
{ id: "1", name: "John", aid:"uuud2905555" },
{ id: "3", name: "Sam", aid:"uuud29029303" },
{ id: "5", name: "Bob", aid:"uuud29435454" },
];
console.log('result as a JSON: ', createJSON(objArray1, objArray2));
.as-console-wrapper { max-height: 100% !important; top: 0 }
Explanation
Inline comments added to the snippet above.
EDIT
Use name and id from array-1. Used restOfArr1Props to account for all other props of array-1 objects to be included.
const createJSON = (arr1, arr2) => (
JSON.stringify({
records: arr1
.filter(
({ id }) => arr2.some(ob => ob.id === id)
)
.map(
({ id : sid, name : Name, ...restOfArr1Props }) => ({
id: arr2.find(a2 => a2.id === sid)?.aid ?? 'missing aid',
fields: {
Name, sid, ...restOfArr1Props
},
})
)
})
);
// SystemA
const objArray1 = [
{ id: "1", name: "John", prop1: 'value11', prop2: 'value12' },
{ id: "2", name: "Jack", prop1: 'value21', prop2: 'value22' },
{ id: "3", name: "Sam", prop1: 'value31', prop2: 'value32' },
{ id: "4", name: "Bill", prop1: 'value41', prop2: 'value42' },
];
// SystemB
const objArray2 = [
{ id: "1", name: "John", aid:"uuud2905555" },
{ id: "3", name: "Sam", aid:"uuud29029303" },
{ id: "5", name: "Bob", aid:"uuud29435454" },
];
console.log('result as a JSON: ', createJSON(objArray1, objArray2));
.as-console-wrapper { max-height: 100% !important; top: 0 }
const objArray1 = [
{ id: '1', name: 'John', type: 'bully' },
{ id: '2', name: 'Jack', type: 'sporty' },
{ id: '3', name: 'Sam', type: 'kind' },
{ id: '4', name: 'Bill', type: 'poliet' },
];
const objArray2 = [
{ id: '1', name: 'John', aid: 'uuud2905555' },
{ id: '3', name: 'Sam', aid: 'uuud29029303' },
{ id: '5', name: 'Bob', aid: 'uuud29435454' },
];
const syncedUsers = { records: [] };
for (let user1 of objArray1) {
const foundUser = objArray2.find(user2 => user2.id === user1.id);
if (foundUser) {
syncedUsers.records.push({
id: foundUser.aid,
fields: { sid: user1.id, name: user1.name, ...user1 },
});
}
}
console.log(JSON.stringify(syncedUsers));
I have an object that I want to remove elements from one of its' properties that is an array of objects based on a matching property of the outer object.
This uses npm deep-diff to compare the two objects.
My problem is inside of combineDuplicateRecords it compares every record against every record, creating duplicates in the identities array. So identities will end up looking like:
[{
id: "111",
identities: [
{
id: "111"
},
{
id: "111"
},
{
id: "222"
},
{
id: "222"
},
{
id: "333"
},
{
id: "333"
}
]
}]
when I really want it to look like this:
[{
id: "111",
identities:[
{
id: "222"
},
{
id: "333"
}
]
}]
Code:
var requestRecords = [
{
vid: "12345",
id: "12345",
email: "gft#test.com",
firstName: "GrandFathering",
lastName: "TestMN",
postalCode: "55443-2410",
phone: "123-456-7890",
key: "1212"
},
{
vid: "121212",
id: "12222",
email: "g233#test.com",
firstName: "NoMatch",
lastName: "NoMatchFound",
postalCode: "43233-2410",
phone: "123-456-7890",
key: "121233"
},
{
vid: "111",
id: "111",
email: "ffffft#test.com",
firstName: "samebatch",
lastName: "samebatch",
postalCode: "5545",
phone: "123-456-7890",
key: "3333",
},
{
vid: "222",
id: "222",
email: "ffffft#test.com",
firstName: "samebatch",
lastName: "samebatch",
postalCode: "5545",
phone: "123-456-7890",
key: "4444",
},
{
vid: "333",
id: "333",
email: "ffffft#test.com",
firstName: "samebatch",
lastName: "samebatch",
postalCode: "5545",
phone: "123-456-7890",
key: "55",
}
];
combineDuplicateRecords = (arrayOfRecords, prefilter) => {
const recordsToRemove = [];
arrayOfRecords.forEach(firstRecord => {
arrayOfRecords.forEach((secondRecord, index) => {
if (
firstRecord.firstName == secondRecord.firstName &&
firstRecord.lastName == secondRecord.lastName &&
firstRecord.dateOfBirth == secondRecord.dateOfBirth &&
firstRecord.phone == secondRecord.phone &&
firstRecord.postalCode == secondRecord.postalCode &&
firstRecord.id !=
secondRecord.id
) {
const identities = [];
let identity = {};
this.preserveExisitingIdentities(secondRecord, identities);
this.preserveExisitingIdentities(firstRecord, identities);
identity = this.setIdentityDifferencesBetweenRecords(
firstRecord,
secondRecord,
prefilter,
identity
);
identities.push(identity);
firstRecord["identities"] = identities;
recordsToRemove.push(index);
}
});
});
[...new Set(recordsToRemove)].forEach(index => {
arrayOfRecords.splice(index, 1);
});
return arrayOfRecords;
};
preserveExisitingIdentities = (record, identities) => {
if (record.hasOwnProperty("identities")) {
record.identities.forEach(identity => {
identities.push(identity);
});
}
return identities;
};
setIdentityDifferencesBetweenRecords = (
firstIdentity,
secondIdentity,
prefilter,
identity
) => {
const differences = Diff(firstIdentity, secondIdentity, prefilter);
let i = differences.length;
while (i--) {
if (differences[i].path[0] == "vid") {
differences.splice(i, 1);
}
if (differences[i].path[0] == "identities") {
differences.splice(i, 1);
}
//we only want to keep the differences so we remove kind D
if (differences[i]?.kind == "D") {
differences.splice(i, 1);
}
}
differences.forEach(diff => {
identity[diff.path[0]] = diff.lhs;
});
return identity;
};
console.log(JSON.stringify(combineDuplicateRecords(requestRecords)));
grab each inner id and save them in a data structure, then use Array#find to find the entire object and insert it back into identities
const array = [
{
id: "111",
identities: [
{
id: "111"
},
{
id: "111"
},
{
id: "222"
},
{
id: "222"
},
{
id: "333"
},
{
id: "333"
}
]
}
]
const cleanObject = (obj) => {
const allIds = obj.identities.map(({ id }) => id)
const mainId = obj.id
const uniqueIds = new Set(allIds)
uniqueIds.delete(mainId)
const nextIdentities = [...uniqueIds].map(currId => {
return obj.identities.find(({ id }) => currId === id)
})
obj.identities = nextIdentities
return obj
};
const el = array.map(entry => {
return cleanObject(entry)
})
console.log(el)
I'm trying to filter a on a nested array inside an array of objects in an Angular app. Here's a snippet of the component code -
var teams = [
{ name: 'Team1', members: [{ name: 'm1' }, { name: 'm2' }, { name: 'm3' }] },
{ name: 'Team2', members: [{ name: 'm4' }, { name: 'm5' }, { name: 'm6' }] },
{ name: 'Team3', members: [{ name: 'm7' }, { name: 'm8' }, { name: 'm9' }] }
];
What I'm trying to achieve is if I search for m5 for example my result should be -
var teams = [
{ name: 'Team1', members: [] },
{ name: 'Team2', members: [{ name: 'm5' }] },
{ name: 'Team3', members: [] }
];
So I've got teams and filteredTeams properties and in my search function I'm doing -
onSearchChange(event: any): void {
let value = event.target.value;
this.filteredTeams = this.teams.map(t => {
t.members = t.members.filter(d => d.name.toLowerCase().includes(value));
return t;
})
}
Now this does work to some extent however because I'm replacing the members it's destroying the array on each call (if that makes sense). I understand why this is happening but my question is what would be the best way to achieve this filter?
you were very close, the only thing that you did wrong was mutating the source objects in teams
basically you can use spread operator to generate a new entry and then return a whole new array with new values.
const teams = [
{ name: 'Team1', members: [{ name: 'm1' }, { name: 'm2' }, { name: 'm3' }] },
{ name: 'Team2', members: [{ name: 'm4' }, { name: 'm5' }, { name: 'm6' }] },
{ name: 'Team3', members: [{ name: 'm7' }, { name: 'm8' }, { name: 'm9' }] }
];
const value = 'm5';
const result = teams.map(t => {
const members = t.members.filter(d => d.name.toLowerCase().includes(value));
return { ...t, members };
})
console.log(result)
Check this. Instead of hard coded m5 pass your value.
const teams = [
{ name: 'Team1', members: [{ name: 'm1' }, { name: 'm2' }, { name: 'm3' }] },
{ name: 'Team2', members: [{ name: 'm4' }, { name: 'm5' }, { name: 'm6' }] },
{ name: 'Team3', members: [{ name: 'm7' }, { name: 'm8' }, { name: 'm9' }] }
];
const filteredTeams = teams.map(team => ({ name: team.name, members: team.members.filter(member => member.name.includes('m5')) }));
console.log(filteredTeams);
You are mutating the original objects, but you could assing new properties to the result object for mapping instead.
var teams = [{ name: 'Team1', members: [{ name: 'm1' }, { name: 'm2' }, { name: 'm3' }] }, { name: 'Team2', members: [{ name: 'm4' }, { name: 'm5' }, { name: 'm6' }] }, { name: 'Team3', members: [{ name: 'm7' }, { name: 'm8' }, { name: 'm9' }] }],
result = teams.map(o => Object.assign(
{},
o,
{ members: o.members.filter(({ name }) => name === 'm5') }
));
console.log(result);
console.log(teams);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Try to seperate your filter function first:
const filterTeamMembers = (teams, filterArr) => {
const useFilter = filterArr.map(x => x.toLowerCase());
return teams.map(team => ({
...team,
members: team.members.filter(member => useFilter.includes(member.name))
}))
};
// =========== And then:
onSearchChange(event: any): void {
let value = event.target.value;
this.filteredTeams = filterTeamMembers(this.teams, [value]);
}
I have the following array with objects and used the following code to creating a tally with the key "id":
var arr=[
{ id: 123,
title: "name1",
status: "FAILED"
},
{
id: 123,
title: "name1",
status: "PASSED"
},
{
id: 123,
title: "name1",
status: "PASSED"
},
{
id: 123,
title: "name1",
status: "PASSED"
},
{
id: 1234,
title: "name2",
status: "FAILED"
},
{
id: 1234,
title: "name2",
status: "PASSED"
}
];
const test =arr.reduce((tally, item) => {
if (!tally[item.id]) {
tally[item.id] = 1;
} else {
tally[item.id] = tally[item.id] + 1;
}
return tally;
}, {});
console.log(test);
Now what I want to do is to modify the tally to take in consideration the key status as well so the result will be somthing like:
[
{id:123, status:"PASSED", tally:3},
{id:123, status:"FAILED", tally:1},
{id:1234, status:"PASSED", tally:1},
{id:1234, status:"FAILED", tally:1}
]
Any idea? Thanks!
Just make the key item.id + item.status, then it's a simple assignment
const res = Object.values(arr.reduce((a, b) => {
a[b.id + b.status] = Object.assign(b, {tally: (a[b.id + b.status] || {tally: 0}).tally + 1});
return a;
}, {}));
console.log(res);
<script>
const arr=[
{ id: 123,
title: "name1",
status: "FAILED"
},
{
id: 123,
title: "name1",
status: "PASSED"
},
{
id: 123,
title: "name1",
status: "PASSED"
},
{
id: 123,
title: "name1",
status: "PASSED"
},
{
id: 1234,
title: "name2",
status: "FAILED"
},
{
id: 1234,
title: "name2",
status: "PASSED"
}
];
</script>
here you go
const test = arr.reduce((acc, item) => {
let found = acc.find(obj => obj.id === item.id && obj.status === item.status)
if (typeof found === "undefined") {
item.tally = 1
acc.push(item);
} else {
found.tally++;
}
return acc;
}, []);
You should group your items first using key that will contain both id and status:
const result = arr.reduce((acc, item) => {
const key = item.id + item.status;
acc[key] = acc[key] || { ...item, tally: 0 };
acc[key].tally++;
return acc;
}, {});
console.log( Object.values(result) );
Output:
[
{ id: 123, title: 'name1', status: 'FAILED', tally: 1 },
{ id: 123, title: 'name1', status: 'PASSED', tally: 3 },
{ id: 1234, title: 'name2', status: 'FAILED', tally: 1 },
{ id: 1234, title: 'name2', status: 'PASSED', tally: 1 },
]
Simply create a key with combination of id and status. And make a map using it. After that you can simply get the desired result from that.
Try the following:
var arr=[{id:123,title:"name1",status:"FAILED"},{id:123,title:"name1",status:"PASSED"},{id:123,title:"name1",status:"PASSED"},{id:123,title:"name1",status:"PASSED"},{id:1234,title:"name2",status:"FAILED"},{id:1234,title:"name2",status:"PASSED"}];
const map =arr.reduce((tally, item) => {
tally[item.id+"_"+item.status] = (tally[item.id+"_"+item.status] || 0) +1;
return tally;
}, {});
const result = Object.keys(map).map((a)=>{
var obj = {
id : a.split("_")[0],
status : a.split("_")[1],
tally : map[a]
};
return obj;
});
console.log(result);