how to apply recursive function on json data in javascript - javascript

I've validation schema in js like this
var data = [
{
id: {
label: "",
tests: [
{
name: "required",
params: "",
},
],
},
},
{
name: {
fields: {
firstName: {
label: "",
tests: [
{
name: "required",
params: "",
},
],
},
lastName: {
label: "",
tests: [
{
name: "required",
params: "",
},
],
},
midName: {
label: "",
tests: [],
},
},
tests: [],
},
},
{
email: {
label: "",
tests: [
{
name: "required",
params: "",
},
],
},
},
{
city: {
label: "",
tests: [],
},
},
{
details: {
fields: {
age: {
label: "",
tests: [
{
name: "max",
params: {
max: 18,
},
},
],
},
tests: [],
},
},
},
];
I only want the name of the keys from array which has required field like this
var selected= ["id", "name", "email"].
For this want to create one dynamic function like if data has key:"tests" 'required' it will return the name and if data has key:"fields" it will check again for the 'tests'.

var data = [
{
id: {
label: "",
tests: [
{
name: "required",
params: "",
},
],
},
},
{
name: {
fields: {
firstName: {
label: "",
tests: [
{
name: "required",
params: "",
},
],
},
lastName: {
label: "",
tests: [
{
name: "required",
params: "",
},
],
},
midName: {
label: "",
tests: [],
},
},
tests: [],
},
},
{
email: {
label: "",
tests: [
{
name: "required",
params: "",
},
],
},
},
{
city: {
label: "",
tests: [],
},
},
{
details: {
fields: {
age: {
label: "",
tests: [
{
name: "max",
params: {
max: 18,
},
},
],
},
tests: [],
},
},
},
]
console.clear()
const keys = data.map(x => recursive(Object.keys(x)[0], x)).filter(x => x !== undefined)
function recursive (name, x) {
for (let key in x) {
if (x[key].tests !== undefined && typeof x[key].tests === "object") {
const find = x[key].tests.find(y => y.name === "required")
if (find) return name
}
if (x[key].fields !== undefined) {
for (let y in x[key].fields) {
if (y !== "tests") {
console.log(y)
const v = recursive(parent, x[key].fields)
if (v !== undefined) return name
}
}
}
}
}
console.log("KEYS", keys)
Like you said search recursive in child if we have only one time require in namefield. Just you need to remember the name of your data and filter at the end, because i don't return a name so that return undefined if i didn't find this specific string field.

Related

merge and remove elements in nested arrays

i have this array, i want to merge all elements inside the objects in the nested arrays and remove the duplicates..
the array is the output of mongo db populate so answers from there or just js will be amazing :)
"visitors": [
[
{
"name": "matan",
"id": "61793e6a0e08cdcaf213c0b1"
},
{
"name": "shani",
"id": "61793e910e08cdcaf213c0b5"
}
],
[
{
"name": "david",
"id": "6179869cb4944c6b19b05a23"
},
{
"name": "orit",
"id": "617986e535fdf4942ef659bd"
}
],
[
{
"name": "david",
"id": "6179869cb4944c6b19b05a23"
},
{
"name": "orit",
"id": "617986e535fdf4942ef659bd"
}
]
]
would like this output -
"visitors": [
{
"name": "matan",
"id": "61793e6a0e08cdcaf213c0b1"
},
{
"name": "shani",
"id": "61793e910e08cdcaf213c0b5"
},
{
"name": "david",
"id": "6179869cb4944c6b19b05a23"
},
{
"name": "orit",
"id": "617986e535fdf4942ef659bd"
},
]
these are my collections
i need to get all visitors on one solar system,
so > solars > planets > visitors
const solarsModel = new Schema({
planets: [ { type: Schema.Types.ObjectId ,ref:'planet'} ],
starName: { type: String, required: true, default: "" }
})
const planetModel = new Schema({
planetName: { type: String, required: true, default: "" },
system:{type: Schema.Types.ObjectId, ref: 'solar'},
visitors: [{ type: Schema.Types.ObjectId , ref: 'visitor'}]
})
const visitorModel = new Schema({
visitorName:{ type: String, required: true, default: "" },
homePlanet: {type: Schema.Types.ObjectId, ref:"planet" },
visitedPlanets: [{ type: Schema.Types.ObjectId, ref:"planet" }]
})
this is what i did to achieve a result would love to use Aggregate..
const response = await solarModel
.findById({ _id: data.id })
.select({ starName: 1, _id: 0 })
.populate({
path: "planets",
select: { visitors: 1, _id: 0 },
populate: {
path: "visitors",
select: "visitorName",
},
})
.exec();
solved with this
exports.findVisitorSystemHandler = async (data) => {
const systemName = await solarModel.findById({ _id: data.id });
const response = await planetModel.aggregate([
{ $match: { system: makeObjectId(data.id) } },
{
$lookup: {
from: "visitors",
localField: "visitors",
foreignField: "_id",
as: "solarVisitors",
},
},
{
$project: {
solarVisitors: {
visitedPlanets: 0,
homePlanet: 0,
__v: 0,
},
},
},
{ $unwind: "$solarVisitors" },
{
$group: {
_id: null,
system: { $addToSet: systemName.starName },
solarVisitors: {
$addToSet: {
id: "$solarVisitors._id",
name: "$solarVisitors.visitorName",
},
},
},
},
{ $unwind: "$system" },
{
$project: {
_id: 0,
},
},
]);
return response;
};
You can use aggregate() like this:
$unwind twice due to nested array
$group using $addToSet to not get duplicates.
db.collection.aggregate([
{
"$unwind": "$visitors"
},
{
"$unwind": "$visitors"
},
{
"$group": {
"_id": null,
"visitors": {
"$addToSet": {
"id": "$visitors.id",
"name": "$visitors.name"
}
}
}
}
])
Example here
(1) Flatten the array of arrays
visitors = visitors.flat();
Which gives us this:
[
{ name: 'matan', id: '61793e6a0e08cdcaf213c0b1' },
{ name: 'shani', id: '61793e910e08cdcaf213c0b5' },
{ name: 'david', id: '6179869cb4944c6b19b05a23' },
{ name: 'orit', id: '617986e535fdf4942ef659bd' },
{ name: 'david', id: '6179869cb4944c6b19b05a23' },
{ name: 'orit', id: '617986e535fdf4942ef659bd' }
]
(2) Get unique ids
let uniqueIds= [...new Set(visitors.map(v => v.id)]
Which gives us this:
[
'61793e6a0e08cdcaf213c0b1',
'61793e910e08cdcaf213c0b5',
'6179869cb4944c6b19b05a23',
'617986e535fdf4942ef659bd'
]
(3) Get new list of visitors based only on uniqueIds
visitors = uniqueIds.map(id => {
let name = visitors.find(v => v.id === id).name;
return {
id,
name
}
});
Which gives us this:
[
{ name: 'matan', id: '61793e6a0e08cdcaf213c0b1' },
{ name: 'shani', id: '61793e910e08cdcaf213c0b5' },
{ name: 'david', id: '6179869cb4944c6b19b05a23' },
{ name: 'orit', id: '617986e535fdf4942ef659bd' },
]
Query
reduce with concat to flatten
union with an empty array,just to remove duplicates
if you have other fields except visitors they are not affected
PlayMongo
aggregate(
[{"$set":
{"visitors":
{"$setUnion":
[{"$reduce":
{"input": "$visitors",
"initialValue": [],
"in": {"$concatArrays": ["$$value", "$$this"]}}},
[]]}}}])
Results
[{
"visitors": [
{
"name": "david",
"id": "6179869cb4944c6b19b05a23"
},
{
"name": "matan",
"id": "61793e6a0e08cdcaf213c0b1"
},
{
"name": "orit",
"id": "617986e535fdf4942ef659bd"
},
{
"name": "shani",
"id": "61793e910e08cdcaf213c0b5"
}
]
}]

create and count dynamic object in typescript

I am trying to compute a result from an array for objects that I can count how many values for each key and add its own dbId to the same computed result within the object name!
the reason behind that is to use it to chart.js. so I can check each result value comes with its own dbIds
const obj = [
{
dbId: 26598,
properties: [
{ attributeName: "BIM7AATypeCode" },
{ displayCategory: "Identity Data" },
{ displayName: "BIM7AATypeCode" },
{ displayValue: "221" },
],
},
{
dbId: 26591,
properties: [
{ attributeName: "BIM7AATypeCode" },
{ displayCategory: "Identity Data" },
{ displayName: "BIM7AATypeCode" },
{ displayValue: "221" },
],
},
{
dbId: 3695,
properties: [
{ attributeName: "abc" },
{ displayCategory: "Identity Data" },
{ displayName: "BIM7AATypeCode" },
{ displayValue: "123" },
],
},
{
dbId: 3697,
properties: [
{ attributeName: "abc" },
{ displayCategory: "Identity Data" },
{ displayName: "BIM7AATypeCode" },
{ displayValue: "123" },
],
},
];
// type Histogram = {
// [key: string]: number;
// };
let histogram = {}; //as Histogram
for (let iterator of obj) {
for (let { displayName, displayValue } of iterator.properties) {
//#ts-ignore
histogram[displayName] = histogram[displayName] | {};
if (!histogram[displayValue]) {
histogram[displayValue] = 1;
} else {
histogram[displayValue] = histogram[displayValue] + 1;
}
}
}
console.log(histogram);
//expected result:
/*
{
BIM7AATypeCode: {
"221": 2,
dbid: [26598, 26591],
},
abc: {
"123": 2,
dbid: [3695, 3697],
},
};
*/
Please find the Array.reduce implementation.
Logic
Loop through obj array using Array.reduce
From each object in the array, pick the properties key. From this select node having key attributeName which is attribute node and with displayValue which is display value node.
Check whether the accumulator has a node with the attributeName.
If not, push a node to accumulator, with the displayValue having vale 1 and dbid as a single noded array.
If accumulator already have this node. Update the count of displayValue and push the new dbid
const obj = [
{ dbId: 26598, properties: [{ attributeName: "BIM7AATypeCode" }, { displayCategory: "Identity Data" }, { displayName: "BIM7AATypeCode" }, { displayValue: "221" }] },
{ dbId: 26591, properties: [{ attributeName: "BIM7AATypeCode" }, { displayCategory: "Identity Data" }, { displayName: "BIM7AATypeCode" }, { displayValue: "221" }] },
{ dbId: 3695, properties: [{ attributeName: "abc" }, { displayCategory: "Identity Data" }, { displayName: "BIM7AATypeCode" }, { displayValue: "123" }] },
{ dbId: 3697, properties: [{ attributeName: "abc" }, { displayCategory: "Identity Data" }, { displayName: "BIM7AATypeCode" }, { displayValue: "123" }] },
];
const output = obj.reduce((acc, curr) => {
const attribute = curr.properties.find(node => node.attributeName);
const displayValue = curr.properties.find(node => node.displayValue);
if (acc[attribute.attributeName]) {
acc[attribute.attributeName][displayValue.displayValue] ? acc[attribute.attributeName][displayValue.displayValue]++ : acc[attribute.attributeName][displayValue.displayValue] = 1;
acc[attribute.attributeName].dbid.push(curr.dbId);
} else {
const newAttribute = {
[displayValue.displayValue]: 1,
dbid: [curr.dbId]
}
acc[attribute.attributeName] = newAttribute;
}
return acc;
}, {});
console.log(output);

How know the value of property of object in javascript?

I have this object:
var x= {
"data": {
"getLand": {
"id": "xxx",
"bid": [{
"result": "ON",
"buyer": {
"username": "Dis"
},
"offerSet": [{
"createdStr": "202",
"value": 1
}]
},
{
"result": "CANCEL",
"buyer": {
"username": "Dis"
},
"offerSet": [{
"createdStr": "202",
"value": 15
}]
}
]
}
}
}
How can i know is result === "ON" && username == "Dis" ?
I tried with this:
for (var key in x.data.getLand.bid) {
if((x.data.getLand.bid[key].result === 'ON') && (x.data.getLand.bid[key].buyer.username.toUpperCase() === 'DIS')){
console.log(x.data.getLand.bid[key]);
}
}
it gives me some problems .... sometimes it works and sometimes it doesn't. Would you be kind enough to show me another way?
You can utilize the ES6 array function forEach to loop through the array items.
const x = { data: { getLand: { id: "xxx", bid: [ { result: "ON", buyer: { username: "Dis", }, offerSet: [ { createdStr: "202", value: 1, }, ], }, { result: "CANCEL", buyer: { username: "Dis", }, offerSet: [ { createdStr: "202", value: 15, }, ], }, ], }, }, }
const bidList = x.data.getLand.bid
bidList.forEach((bid, index) => {
if (bid.result === 'ON' && bid.buyer.username.toUpperCase() === 'DIS') console.log(index, bid)
})
You can do this with filter and forEach loop.
like this:
var x = { data: { getLand: { id: "xxx", bid: [ { result: "ON", buyer: { username: "Dis" }, offerSet: [{ createdStr: "202", value: 1 }] }, { result: "CANCEL", buyer: { username: "Dis" }, offerSet: [{ createdStr: "202", value: 15 }] } ] } } };
x.data.getLand.bid
.filter(
({ result, buyer: { username } }) => result === "ON" || username === "Dis"
)
.forEach((el, id) => console.log(el, id));
An example with for...of
var x = { data: { getLand: { id: "xxx", bid: [ { result: "ON", buyer: { username: "Dis", }, offerSet: [ { createdStr: "202", value: 1, }, ], }, { result: "CANCEL", buyer: { username: "Dis", }, offerSet: [ { createdStr: "202", value: 15, }, ], }, ], }, }, };
for (const item of x.data.getLand.bid) {
if (item.result === "ON" && item.buyer.username.toUpperCase() === "DIS") {
console.log(item);
}
}
Edit:
If you need the index, you can use forEach.
var x = { data: { getLand: { id: "xxx", bid: [ { result: "ON", buyer: { username: "Dis", }, offerSet: [ { createdStr: "202", value: 1, }, ], }, { result: "CANCEL", buyer: { username: "Dis", }, offerSet: [ { createdStr: "202", value: 15, }, ], }, ], }, }, };
x.data.getLand.bid.forEach((item, i) => {
if (item.result === "ON" && item.buyer.username.toUpperCase() === "DIS") {
console.log(i);
console.log(item);
}
});

filter through multiple arrays and add objects from them to a single array

Example of an object in the accounts array:
const accounts = [
{
id: "5f446f2ecfaf0310387c9603",
picture: "https://api.adorable.io/avatars/75/esther.tucker#zillacon.me",
age: 25,
name: {
first: "Esther",
last: "Tucker",
},
company: "ZILLACON",
email: "esther.tucker#zillacon.me",
registered: "Thursday, May 28, 2015 2:51 PM",
},
Example of an object in the books array:
const books = [
{
id: "5f447132d487bd81da01e25e",
title: "sit eiusmod occaecat eu magna",
genre: "Science",
authorId: 8,
borrows: [
{
id: "5f446f2e2cfa3e1d234679b9",
returned: false,
},
{
id: "5f446f2ed3609b719568a415",
returned: true,
},
{
id: "5f446f2e1c71888e2233621e",
returned: true,
},
{
id: "5f446f2e6059326d9feb9a68",
returned: true,
},
{
id: "5f446f2ede05a0b1e3394d8b",
returned: true,
},
{
id: "5f446f2e4081699cdc6a2735",
returned: true,
},
{
id: "5f446f2e3900dfec59489477",
returned: true,
},
{
id: "5f446f2e6059326d9feb9a68",
returned: true,
},
{
id: "5f446f2e409f8883af2955dd",
returned: true,
},
{
id: "5f446f2e3900dfec59489477",
returned: true,
},
{
id: "5f446f2eae901a82e0259947",
returned: true,
},
{
id: "5f446f2ef2ab5f5a9f60c4f2",
returned: true,
},
{
id: "5f446f2ea6b68cf6f85f6e28",
returned: true,
},
{
id: "5f446f2eed18105706d6ca19",
returned: true,
},
{
id: "5f446f2eae901a82e0259947",
returned: true,
},
{
id: "5f446f2e91c2af00cb74e82b",
returned: true,
},
{
id: "5f446f2e5aa2bb5545a0f8a6",
returned: true,
},
{
id: "5f446f2ea508b6a99c3e42c6",
returned: true,
},
{
id: "5f446f2e50cc2da9cd80efdb",
returned: true,
},
{
id: "5f446f2e0b3e2ff72fc503e7",
returned: true,
},
{
id: "5f446f2e91c2af00cb74e82b",
returned: true,
},
{
id: "5f446f2ef795e593cd3cd19d",
returned: true,
},
{
id: "5f446f2e2f35653fa80bf490",
returned: true,
},
{
id: "5f446f2e7b9cd304fed3a8bc",
returned: true,
},
{
id: "5f446f2ed9aac23c0340aab2",
returned: true,
},
],
},
Example of objects in the authors array:
const authors = [
{
id: 0,
name: {
first: "Lucia",
last: "Moreno",
},
},
{
id: 1,
name: {
first: "Trisha",
last: "Mathis",
},
},
{
id: 2,
name: {
first: "Arnold",
last: "Marks",
},
},
I need to write the function function getBooksPossessedByAccount(account, books, authors) {} that does the following: It returns an array of books and authors that represents all books currently checked out by the given account. Look carefully at the object below, as it's not just the book object; the author object is embedded inside of it.
Output example:
getBooksPossessedByAccount(account, books, authors);
[
{
id: "5f447132320b4bc16f950076",
title: "est voluptate nisi",
genre: "Classics",
authorId: 12,
author: {
id: 12,
name: {
first: "Chrystal",
last: "Lester",
},
},
borrows: [
{
id: "5f446f2e6059326d9feb9a68",
returned: false,
},
...
],
},
]
Here's what I have so far:
function getBooksPossessedByAccount(account, books, authors) {
const accId = account.id;
const result = [];
for (let idxBks = 0; idxBks < books.length; idxBks++) {
if (
books[idxBks].borrows.id === accId &&
books[idxBks].borrows.returned === false
) {
result.push(books[idxBks]);
}
for (let idxAuth = 0; idxAuth < authors.length; idxAuth++) {
let authorIdx = authors[idxAuth];
if (authorIdx.id === result.authorId) {
return [result, { author: authorIdx }];
}
}
}
return result;
}
You need to search all the borrows, not just borrows[0]. You can use the some() method to check all of them.
Since the author information needs to be added as a property to the book object, you shouldn't be pushing it onto the booksOut array.
function getBooksPossessedByAccount(account, books, authors) {
const accId = account.id;
const booksOut = books.filter(
(book) => book.borrows.some(borrow => !borrow.returned && borrow.id === accId)
);
booksOut.forEach(book => book.author = authors.find(author => book.authorID == author.id))
return booksOut;
}
Using some should do the trick..
function getBooksPossessedByAccount(account, books, authors) {
let borrowedBooks=books.filter(book=>
book.some(borrow=>borrow.id===account.id)
)
return borrowedBooks //array of book objects
//return borrowedBooks.map(book=>book.id) //to show array of book ids
}

How to modify properties of nested objects?

Lets say I have the following object with nested objects:
const obj = {
Visualization: {
Lower: [{ name: "Part", selectedValue: "60-000" }],
Upper: [{ name: "Part", selectedValue: "60-000" }],
},
Holder: {
Lower: [
{ name: "Part", selectedValue: "30-000" },
{ name: "Project Name", selectedValue: "45-000" },
],
Upper: [
{ name: "Part", selectedValue: "22-000" },
{ name: "Project Name", selectedValue: "25-000" },
],
},
};
And I want to change the selectedValue of all of them at once to "0" and have the following result:
{
Visualization: {
Lower: [{ name: "Part", selectedValue: "0" }],
Upper: [{ name: "Part", selectedValue: "0" }],
},
Holder: {
Lower: [
{ name: "Part", selectedValue: "0" },
{ name: "Project Name", selectedValue: "0" },
],
Upper: [
{ name: "Part", selectedValue: "0" },
{ name: "Project Name", selectedValue: "0" },
],
},
}
How can I make it properly?
Thank you!
function deepSet(o, propertyName, propertyValue) {
Object.keys(o).forEach(key => {
if (key === propertyName) {
o[key] = propertyValue;
return;
}
if (Array.isArray(o[key])) {
o[key].forEach(item => {
deepSet(item, propertyName, propertyValue);
});
return;
}
if (typeof o[key] !== 'object') {
return;
}
deepSet(o[key], propertyName, propertyValue);
});
}
JSFiddle: https://jsfiddle.net/713vfdrg/
You can handle this recursively and update the original object.
All I did was check to see if the object is an Array. If it is then you loop through the array and update each objects selectedValue to be 0. This will update those keys arrays.
If it is not an array then that means you are dealing with an object, either the parent or children object, so then you loop over the keys and drill down the object structure for each key.
This function below assumes that the data will be in that format you have shown above.
const obj = {
Visualization: {
Lower: [{ name: "Part", selectedValue: "60-000" }],
Upper: [{ name: "Part", selectedValue: "60-000" }],
},
Holder: {
Lower: [
{ name: "Part", selectedValue: "30-000" },
{ name: "Project Name", selectedValue: "45-000" },
],
Upper: [
{ name: "Part", selectedValue: "22-000" },
{ name: "Project Name", selectedValue: "25-000" },
],
},
};
function updateSelectedValue(obj, keyToUpdate, valueUpdate) {
if(Array.isArray(obj)){
obj.forEach((content, idx) => {
if(obj[idx][keyToUpdate]){
obj[idx][keyToUpdate] = valueUpdate;
}
})
return;
}
const keys = Object.keys(obj);
for(const key in obj){
updateSelectedValue(obj[key], keyToUpdate, valueUpdate);
}
}
updateSelectedValue(obj, "selectedValue", 0)
console.log(obj)

Categories

Resources