Javascript looping through object to find match pair url - javascript

I am working on a reactjs project - where I have say an English url like "/en/how-it-works" and wish to loop through the json tree to find the position/child level at which this link is set -- and find its German matching pair.
So basically -- a function that is given the English url, returns the German url
so --
lng - return de
currenlang - en
pairUrl - /en/how-it-works
return /de/anleitung
or
lng - return en
currentlang de
pairUrl - /de/beliebte-projekte/bundle1
return /en/popular-projects/bundle1
//function
getLanguagePair (lng, currentLng, pairUrl) {
// 'find url in json tree'
console.log('linkTreeObject', linkTreeObject.langs)
var obj = {}
//find position in tree
if(currentLng === 'de'){
obj = linkTreeObject.langs[0].lines.menu
} else {
obj = linkTreeObject.langs[1].lines.menu
}
var pos = []
for (var k in obj) {
if (!obj.hasOwnProperty(k)) continue
if (obj[k].link === pairUrl) {
pos[k]
}
}
console.log('pos' , pos)
if (lng === 'de') {
return '/de/link'
} else {
return '/en/link'
}
}
//json file
{
"langs" : [
{
"lang" : "de",
"lines" : {
"menu" : [
{
"title": "Anleitung",
"link": "/de/anleitung",
"children" : []
},
{
"title": "Beliebte Projekte",
"link": "/de/beliebte-projekte",
"children" : [
{
"title" : "Bundle1",
"link" : "/de/beliebte-projekte/bundle1"
},
{
"title" : "Bundle2",
"link" : "/de/beliebte-projekte/bundle2"
}
]
}
],
"sign_in" : "Login"
}
},
{
"lang" : "en",
"lines" : {
"menu" : [
{
"title": "How it works",
"link": "/en/how-it-works",
"children" : []
},
{
"title": "Popular Projects",
"link": "/en/popular-projects",
"children" : [
{
"title" : "Bundle1",
"link" : "/en/popular-projects/bundle1"
},
{
"title" : "Bundle2",
"link" : "/en/popular-projects/bundle2"
}
]
}
],
"sign_in" : "Sign in"
}
}
]
}

You will have to loop through 'en' and 'de' simultaneously and use recursion since you have nested links. Here is my version. I already took out the 'en' and 'de' array and used them in the function.
var obj ={
"langs" : [
{
"lang" : "de",
"lines" : {
"menu" : [
{
"title": "Anleitung",
"link": "/de/anleitung",
"children" : []
},
{
"title": "Beliebte Projekte",
"link": "/de/beliebte-projekte",
"children" : [
{
"title" : "Bundle1",
"link" : "/de/beliebte-projekte/bundle1"
},
{
"title" : "Bundle2",
"link" : "/de/beliebte-projekte/bundle2"
}
]
}
],
"sign_in" : "Login"
}
},
{
"lang" : "en",
"lines" : {
"menu" : [
{
"title": "How it works",
"link": "/en/how-it-works",
"children" : []
},
{
"title": "Popular Projects",
"link": "/en/popular-projects",
"children" : [
{
"title" : "Bundle1",
"link" : "/en/popular-projects/bundle1"
},
{
"title" : "Bundle2",
"link" : "/en/popular-projects/bundle2"
}
]
}
],
"sign_in" : "Sign in"
}
}
]
};
var en = obj.langs[1].lines.menu;
var de = obj.langs[0].lines.menu;
function GetUrl(enUrl, enMenu, deMenu)
{
var deUrl;
for(var i = 0; i < enMenu.length; i++)
{
if(enMenu[i].link == enUrl)
{
deUrl = deMenu[i].link;
break;
}
else
{
if(enMenu[i].children && enMenu[i].children.length > 0)
{
deUrl = GetUrl(enUrl,enMenu[i].children, deMenu[i].children )
}
}
}
return deUrl;
}
console.log(GetUrl("/en/how-it-works", en, de));
console.log(GetUrl("/en/popular-projects", en, de));
console.log(GetUrl("/en/popular-projects/bundle1", en, de));
console.log(GetUrl("/en/popular-projects/bundle2", en, de));

Related

How to transform this specific js array into js object?

I have one javascript array got from back end api, for convenience, need to be sort into the form of below, described as final target.
But I don't know how to start. Anyone can help?
The original src array is like below :
var src = [
{
"parent_kind" : "Animal",
"name" : "Cow"
},
{
"name" : "Animal"
},
{
"parent_kind" : "Animal",
"name" : "Dog"
},
{
"parent_kind" : "Animal",
"name" : "Horse"
},
{
"name" : "Vehicle"
},
{
"parent_kind" : "Vehicle",
"name" : "Bus"
},
{
"parent_kind" : "Bus",
"name" : "Shuttle"
},
]
The final target is :
{
"Vehicle" : {
"Bus" : {
"Shuttle" : {}
}
},
"Animal" : {
"Cow" : {},
"Dog" : {},
"Horse" : {}
}
}
I can got each element of the original array by
for (let ele of src) {
console.log(ele)
}
you can do that with a simple Array.reduce() method
var src =
[ { parent_kind: 'Animal', name: 'Cow' }
, { name: 'Animal' }
, { parent_kind: 'Animal', name: 'Dog' }
, { parent_kind: 'Animal', name: 'Horse' }
, { name: 'Vehicle' }
, { parent_kind: 'Vehicle', name: 'Bus' }
, { parent_kind: 'Bus', name: 'Shuttle' }
]
let res = src.reduce((a,{parent_kind,name},i)=>
{
if(!!parent_kind)
{
let np = a.p.find(x=>x.pN===parent_kind)
if(!np)
{
a.r[parent_kind] = {}
np = {pN:parent_kind, e:a.r[parent_kind]}
a.p.push( np )
}
let z = np.e[name] = {}
a.p.push( {pN:name, e:z} )
}
return (i===a.len)? a.r : a
}
,{len:src.length-1,r:{},p:[]})
console.log( res )
.as-console-wrapper { max-height: 100% !important; top: 0; }
if you can ok with loop the src many times :-( That's performance is not a concern.
var src = [{
"parent_kind": "Animal",
"name": "Cow"
},
{
"name": "Animal"
},
{
"parent_kind": "Animal",
"name": "Dog"
},
{
"parent_kind": "Animal",
"name": "Horse"
},
{
"name": "Vehicle"
},
{
"parent_kind": "Vehicle",
"name": "Bus"
},
{
"parent_kind": "Bus",
"name": "Shuttle"
},
];
const fn = (source, result, parent) => {
const children = source.filter(({
parent_kind
}) => parent_kind === parent).map(({
name
}) => name);
children.forEach(c => result[c] = {});
children.forEach(c => fn(source, result[c], c));
}
result = {};
fn(src, result)
console.log(result);

Getting empty data while trying to get desired format of object

I have an object
"data" : [
{
"name" : "Heading",
"text" : "Text Heading",
"type" : "string",
"values" : [
"Arthur"
]
},
{
"name" : "Source",
"text" : "Source Reference",
"type" : "string",
"values" : [
"Jhon"
]
},
{
"name" : "Place",
"text" : "Bank Building",
"type" : "string",
"values" : [
"Mark"
]
},
{
"name" : "Animal",
"text" : "Branch",
"type" : "string",
"values" : [
"Susan"
]
}
]
there is a function i am passing the object and an array as the arguments
fieldArray=["Heading", "Animal"]
myFunction(fieldArray, data){
... your code here
}
I need to get the output in the below format where I have to search the object with the fields in myArray with the name key of data. Then I need to put the value of the searched object in the below format
[{
"id": 1,
"cells": [{
"id": "ConstId",
"cellContent": "Heading"
},
{
"id": "ConstValue",
"cellContent": "Arthur"
}
]
},
{
"id": 2,
"cells": [{
"id": "ConstId",
"cellContent": "Animal"
},
{
"id": "ConstValue", //a constant field name as ConstValue
"cellContent": "Susan" // the value of the second field in the myArray from object with name Animal
}
]
}
]
I have tried this
const getFormattedData = (fieldArray: any, data: any) => {
let innerData: any = [];
for (let i=0; i<fieldArray.length; i++){
const indexNumber = data.find((key: any) => key.name === fieldArray[i])
if(indexNumber != undefined){
innerData.push({
id: i+1,
cells:[{
id: 'inquiryName',
cellContent: indexNumber.name
},
{
id: 'value',
cellContent: indexNumber.values.toString()
}
]
})
}
console.log('innerData :>> ', innerData);
}
}
You could use the below. Since you tagged javascript, posting answer in JS.
function formatData(data, fieldArray) {
let ret = [];
fieldArray.forEach((field, i) => {
let dataObj = data.filter(d => d.name === field)[0]
if( dataObj ) {
ret.push({
"id": 1,
"cells": [{
"id": "ConstId",
"cellContent": field
},
{
"id": "ConstValue",
"cellContent": dataObj.values[0] //Put whole obj or just first
}
]
})
}
})
return ret;
}
Link to plnkr

Simplify forEach statement to update a mongoDB document with nested objects and arrays

I'd like to update the value of the key shouldSendAlert in the following document:
{
"_id" : ObjectId("5c61c4db46d18e1092c5b024"),
"service" : "SRVPVD",
"menu" : [
{
"sub" : [
{
"options" : [
{
"item" : [
{
"name" : "",
"actions" : [
{
"name" : "communicateClient",
"value" : true
},
{
"name" : "shouldSendAlert",
"value" : false
}
]
}
],
"name" : "Technology Support"
},
{
"item" : [
{
"name" : "",
"actions" : [
{
"name" : "communicateClient",
"value" : true
}
]
}
],
"name" : "Company Support"
}
],
"name" : "Support"
},
{
"name" : " FAQ"
}
],
"name" : "Help"
}
]
}
I've managed to do this, querying the document using a multiple $elemMatch query, and using forEach to run through the nested arrays in order to alter the value of shouldSendAlert:
{
let menuItems = db.getCollection('menumodels').find({menu: {$elemMatch: {name: 'Help',sub: {$elemMatch: {name: 'Support',motivos: {$elemMatch: {name: 'Technology Support'}}}}}}});
menuItems.forEach((r) => {
r.menu.forEach(menuItem => {
if (menuItem.name == 'Help') {
menuItem.sub.forEach(sub => {
if (sub.name == 'Support') {
sub.motivos.forEach(motivo => {
if (motivo.name == "Technology Support") {
motivo.item[0].actions.forEach(action => {
if (action.name == 'shouldSendAlert') {
action.value = true;
db.getCollection('menumodels').update({_id: r._id}, {$set: {menu: r.menu}})
}
})
}
})
}
})
}
})
});
}
Is it - regarding performance - necessary, to code this MongoDB query
or update logic into a smarter form? Does the multiple use of $elemMatch affect performance?

javascript update mongodb documents multiple fields by looking up from another collection

I have several hundred thousands of documents in mongoDB to update.
here is an example of existing documents from collection Users:
{
"_id" : "549120bcf5115900124fb6e1",
"user" : "Tom",
"country" : "United Kingdom",
"province" : "North Yorkshire",
"city" : "York",
"organization" : ""
},
{
"_id" : "143184fbf5482260184ac6e2",
"user" : "Jack",
"country" : "Not Listed",
"province" : "",
"city" : "",
"organization" : "United Nations"
},
{
"_id" : "1234567890123456748979",
"user" : "Sarah",
"country" : "Not Listed",
"province" : "",
"city" : "",
"organization" : ""
},
{
"_id" : "98765432411654987654",
"user" : "Mat"
}
Each document has the possibility to have values in these fields :
a country, a province, and a city
or a country and a state
and here is the sample from another collection Countries:
{
"_id" : "123456789",
"key" : "Not Listed",
"uuid" : "ca55b53a-ef5b-43ed-90ed-b857f45ddb6d",
"organization" : [
{
"key" : "United Nations",
"uuid" : "1c4ae4c6-00c5-405d-98fa-ca7cc9edc72a"
},
{
"key" : "FIFA",
"uuid" : "11cfe606-821f-40fb-b1d0-bb7f9abb21dc"
}
],
"province" : [],
},
{
"_id" : "1123465498742",
"key" : "United Kingdom",
"uuid" : "d756e167-25ec-4aa9-b231-4dbf6d4bfce4",
"organization" : [],
"province" : [
{
"key" : "North Yorkshire",
"uuid" : "73d07c77-eba4-4dfa-9ada-e0ba8d8a2d55",
"city" : [
{
"key" : "York",
"uuid" : "80fd18a6-c4eb-4fb9-b591-6cca62319ba7"
},
{
"key" : "Middlesbrough",
"uuid" : "26a277c4-8640-4959-a64a-00f3727975f4"
}
],
},
{
"key" : "Oxfordshire",
"uuid" : "f7b5a570-df42-4520-ba3a-8bdcdd00e7d4",
"city" : [
{
"key" : "Oxford",
"uuid" : "b931865c-a363-4958-b7e7-5503fe674eb0"
},
{
"key" : "Banbury",
"uuid" : "b8d4c63a-75a9-4c3c-a4cd-d315f06a92e0"
}
],
}
]
}
The idea is to look up the country/organization/province/city field value from documents in Users collection and update them based on the uuid value of the Countries collection.
So the result will look like something like this:
{
"_id" : "549120bcf5115900124fb6e1",
"user" : "Tom",
"country" : "d756e167-25ec-4aa9-b231-4dbf6d4bfce4", // uuid of United Kingdom
"province" : "73d07c77-eba4-4dfa-9ada-e0ba8d8a2d55", // uuid of North Yorkshire
"city" : "80fd18a6-c4eb-4fb9-b591-6cca62319ba7", // uuid of York
"state" : ""
},
{
"_id" : "143184fbf5482260184ac6e2",
"user" : "Jack",
"country" : "ca55b53a-ef5b-43ed-90ed-b857f45ddb6d", // uuid of Not Listed
"province" : "",
"city" : "",
"state" : "1c4ae4c6-00c5-405d-98fa-ca7cc9edc72a" // uuid of United Nations
},
{
"_id" : "1234567890123456748979",
"user" : "Sarah",
"country" : "ca55b53a-ef5b-43ed-90ed-b857f45ddb6d", // uuid of Not Listed
"province" : "",
"city" : "",
"state" : ""
},
{
"_id" : "98765432411654987654",
"user" : "Mat"
}
The dependency of the fields are the following:
Country > Province > City
Or:
Country > Organization
It is possible that a parent field exists, but its child field doesn't exist or is empty.
How can I update these multidimensional arrays using mongo script rules?
Here is my attempt, but this is a lot of for loops, and not sure how to do the mongodb find/update/save part.. could somebody help to achieve it?
var usrCountry, uuidcountry, usrProvince, uuidprovince, usrOrg, uuidorg, usrCity, uuidcity;
for (var i = 0; i < users.length; i++) {
usrCountry = users[i].country;
usrProvince = users[i].province;
usrOrg = users[i].organization;
usrCity = users[i].city;
for (var j = 0; j < countries.length; j++) {
if (countries[j].key === usrCountry) {
uuidcountry = countries[j].uuid;
console.log('uuidcountry: ', uuidcountry)
if (countries[j].province.length){
for (var k = 0; k < countries[j].province.length; k++) {
if (countries[j].province[k].key === usrProvince){
uuidprovince = countries[j].province[k].uuid;
console.log('uuidprovince', uuidprovince)
for (var l = 0; l < countries[j].province[k].city.length; l++) {
if (countries[j].province[k].city[l].key === usrCity){
uuidcity = countries[j].province[k].city[l].uuid
console.log('uuidcity: ', uuidcity)
}
}
}
}
}
}
}
}
You can try do this with aggregation pipeline, and use that info to update
db.u.aggregate(
[
{
$lookup: {
from : "c",
localField : "country",
foreignField : "key",
as : "countryInfo"
}
},
{
$project: {
"_id" : 1,
"user" : 1,
"province" : 1,
"country" : 1,
"city" : 1,
"organization" : 1,
"country_uuid" : {$arrayElemAt : ["$countryInfo.uuid",0]},
"province_uuid" : { $arrayElemAt : [{ $map : { input : { $filter : { input : {$arrayElemAt : ["$countryInfo.province" ,0 ]} , as : "pro", cond : { $eq : [ "$$pro.key", "$province" ] } } } , as : "pr", in : "$$pr.uuid" } }, 0 ] },
"city_uuid" : {$arrayElemAt : [{$map : { input : { $arrayElemAt : [ {$filter : { input : { $map : { input : { $arrayElemAt : ["$countryInfo.province.city" ,0 ] }, as : "ct", in : { $filter : { input : "$$ct" , as : "ctyy", cond : { $eq : ["$$ctyy.key", "$city"] } } } } }, as : "o", cond : {$ne : [ {$size : "$$o"} , 0 ] } } } , 0]}, as : "o", in :"$$o.uuid"}}, 0]}
}
}
]
)
result
> db.u.aggregate( [ { $lookup: { from : "c", localField : "country", foreignField : "key", as : "countryInfo" } }, { $project: { "_id" : 1, "user" : 1, "province" : 1, "country" : 1, "city" : 1, "organization" : 1, "country_uuid" : {$arrayElemAt : ["$countryInfo.uuid",0]}, "province_uuid" : { $arrayElemAt : [{ $map : { input : { $filter : { input : {$arrayElemAt : ["$countryInfo.province" ,0 ]} , as : "pro", cond : { $eq : [ "$$pro.key", "$province" ] } } } , as : "pr", in : "$$pr.uuid" } }, 0 ] }, "city_uuid" : {$arrayElemAt : [{$map : { input : { $arrayElemAt : [ {$filter : { input : { $map : { input : { $arrayElemAt : ["$countryInfo.province.city" ,0 ] }, as : "ct", in : { $filter : { input : "$$ct" , as : "ctyy", cond : { $eq : ["$$ctyy.key", "$city"] } } } } }, as : "o", cond : {$ne : [ {$size : "$$o"} , 0 ] } } } , 0]}, as : "o", in :"$$o.uuid"}}, 0]} } } ] ).pretty()
{
"_id" : "549120bcf5115900124fb6e1",
"user" : "Tom",
"country" : "United Kingdom",
"province" : "North Yorkshire",
"city" : "York",
"organization" : "",
"country_uuid" : "d756e167-25ec-4aa9-b231-4dbf6d4bfce4",
"province_uuid" : "73d07c77-eba4-4dfa-9ada-e0ba8d8a2d55",
"city_uuid" : "80fd18a6-c4eb-4fb9-b591-6cca62319ba7"
}

Compare,add,update,delete elements on array of mongodb

Need help on operation like update,delete,add,upsert,delete on below document of MongoDB.
Below is MongoDB document that exists in temp collection.
{
"local_id" : "1841",
"name_first" : "tiger",
"name_last" : "lion",
"address" : [
{
"id" : 1,
"address_type" : "Home",
"city" : "Delhi",
"country" : "",
"po_box" : ""
},
{
"id" : 2,
"address_type" : "Work",
"city" : "",
"country" : "",
"po_box" : ""
}
],
"email" : [
{
"email_id" : "blah#gmail.com",
"id" : 1,
"type" : "Home"
},
{
"email_id" : "Pearl1#gmail.com",
"id" : 2,
"type" : "Work"
}
],
"phone_number" : [
{
"id" : 1,
"no" : "+911234567890",
"type" : "Mobile"
},
{
"id" : 2,
"no" : "+917894561230",
"type" : "work"
}
]
}`
Now I have some document like below, i want query that will compare,add,update,delete on my above document.
`
{
"local_id" : "1730",
"name_first" : "lion",
"name_last" : "king",
"address" : [
{
"id" : 1,
"address_type" : "Home",
"city" : "Delhi",
"country" : "India",
"po_box" : "110041"
},
{
"id" : 2,
"address_type" : "Work",
"city" : "Delhi-NCR",
"country" : "India",
"po_box" : "110048"
},
{
"id" : 3,
"address_type" : "Work",
"city" : "Delhi-NCR",
"country" : "Indai",
"po_box" : "110048"
}
],
"email" : [
{
"email_id" : "updatethis#gmail.com",
"id" : 1,
"type" : "Home"
},
{
"email_id" : "Pearl1#gmail.com",
"id" : 2,
"type" : "Work"
},
{
"email_id" : "addthisarray#gmail.com",
"id" : 3,
"type" : "personal"
}
],
"phone_number" : [
{
"id" : 1,
"no" : "+911234567890",
"type" : "Mobile"
}
/*second array not here so remove that array from that document*/
]
}`
You can save function on server as you can call that function to get the differences, as below.
db.system.js.save({
_id: "getupdatedArray",
value: function(obj1, obj2) {
var VALUE_CREATED = 'created';
var VALUE_UPDATED = 'updated';
var VALUE_DELETED = 'deleted';
var VALUE_UNCHANGED = 'unchanged';
function map(obj1, obj2) {
if (isValue(obj1) || isValue(obj2)) {
return {
type: compareValues(obj1, obj2),
old: obj1,
new: obj2
};
}
var diff = {};
for (var key in obj1) {
if (isFunction(obj1[key])) {
continue;
}
var value2 = undefined;
if ('undefined' != typeof(obj2[key])) {
value2 = obj2[key];
}
diff[key] = map(obj1[key], value2);
}
for (var key in obj2) {
if (isFunction(obj2[key]) || ('undefined' != typeof(diff[key]))) {
continue;
}
diff[key] = map(undefined, obj2[key]);
}
return diff;
}
function compareValues(value1, value2) {
if (value1 === value2) {
return VALUE_UNCHANGED;
}
if ('undefined' == typeof(value1)) {
return VALUE_CREATED;
}
if ('undefined' == typeof(value2)) {
return VALUE_DELETED;
}
return VALUE_UPDATED;
}
function isFunction(obj) {
return {}.toString.apply(obj) === '[object Function]';
}
function isArray(obj) {
return {}.toString.apply(obj) === '[object Array]';
}
function isObject(obj) {
return {}.toString.apply(obj) === '[object Object]';
}
function isValue(obj) {
return !isObject(obj) && !isArray(obj);
}
return map(obj1, obj2);
}
})
Then you can call function as below..
db.loadServerScripts();
getupdatedArray({"a": "abc"}, {"a": "a111", "b": "bbb"});
This will give you result as below:
{
"a" : {
"type" : "updated",
"old" : "abc",
"new" : "a111"
},
"b" : {
"type" : "created",
"old" : undefined,
"new" : "bbb"
}
}
Thanks
Satish Lakhani

Categories

Resources