How to compare object (json) with Model(javascript class) - javascript

I'm using angular 2 at the moment.
And I have array of some data
data: MyModel[] = [
{
id: 1,
name: 'Name',
secondName: 'SecondName'
}
Also MyModel is interface:
interface MyModel {
id: number,
name: string,
secondName: string
Let's imagine, that I received data from Back-End(object json):
{
id: 2,
FIRSTname: 'FName',
secondName: 'SecondName'
}
How can I validate, that keys in object are equals to my interface?
For example field "FIRSTname" is incorrect, then I should throw exception or something else.

Maybe there is a better way in typescript, in es6 you can do:
const data = [
{
id: 2,
name: 'FName',
secondName: 'SecondName'
},
{
name: 'foo',
secondName: 'bar'
},
];
const validate = ({id, name, secondName}) => id && name && secondName
const validData = data.filter(validate)
console.log(validData);
Be a ware that if one of the values: id, name, secondName, is null validate will return false.

What if I say that you can define a variable obj implementing the model interface and initialise all its property and then compare the keys of obj with the backend data lets name it respDataObj
Array.prototype.compare = function(testArr) {
if (this.length != testArr.length) return false;
for (var i = 0; i < testArr.length; i++) {
if (this[i].compare) { //To test values in nested arrays
if (!this[i].compare(testArr[i])) return false;
}
else if (this[i] !== testArr[i]) return false;
}
return true;
}
Object.keys(respDataObj).compare(Object.keys(obj))
Compare function courtesy: https://stackoverflow.com/a/6229258/2791802

Related

Node Js how to fetch data from database in an hierarchical way

I'm writing a back code using NodeJs to fetch some data from backend, I want dataBase data to be like this
like this:
data = [{
name: "Admin",
id: '1',
children: [
{ name: "Admin", id: "1" },
{ name: "groupe1", id: "2" },
{
name: "groupe2", id: "1455", children: [
{ name: "groupe2", id: "1455" },
{ name: "gro", id: "5444" },
{ name: "hhrr", id: "45" }
]
}
]
}]
the idea is simple we have a list of group each group has a parent I want to display all the groups list in an hierarchical way the top one of the tree is done
Some groups are parents and groups in the same time and some others are only groups if the group is not parent we add an object with its name and ID in the array of children of his parent
if this groups is a parent that's mean it has children we add an object with its ID and name in the array of children of his parents, and we add property children for the object which is array named children with for the first time an object with the name and the id of the group etc...
i tryed to do this but it did not work
const getParentsByType = async ({ name, _id }) => {
let parentResult = [
{
id: _id,
name: name,
children: [
{
id: _id,
name: name,
},
],
},
];
parentResult= await findParent(_id, parentResult[0].children, 0);
return parentResult;
};
const findParent = async (parentId, parentResult, itemPos) => {
let children = await Models.GroupModel.find({ parent: parentId, status: true }).select('name _id');
for (let i = 0; i < children.length; i++) {
let childrenList = await Models.GroupModel.find({ parent: children[i]._id, status: true }).select('name _id');
if (childrenList.length != 0) {
parentResult.push(buildParentWithChild(children[i]._id, children[i].name));
findParent(children[i]._id,parentResult.children[i],itemPos++)
} else {
parentResult.push(buildParent(children[i]._id, children[i].name));
}
}
return parentResult
};
and this the model of the data base
const Group = mongoose.Schema({
name: {
type: String,
required: true,
},
status: {
type: Boolean,
required: true,
},
parent: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Group',
},
});
i had two days trying to resolve tis but with no result
i need some helps and Thank you
Try parsing your returned data. It validates your data as objects i dont see any problem with your function regardless i still have no idea what format your a trying to build.
let children = JSON.parse(JSON.stringify(await Models.GroupModel.find({ parent: parentId, status: true }).select('name _id')));
let childrenList = JSON.parse(JSON.stringify(await Models.GroupModel.find({ parent: children[i]._id, status: true }).select('name _id')));
If I understand you right, you want to convert the array returned by Models.GroupModel.find, and which looks like
var dbresult = [
{_id: "1", parent: null, name: "one"},
{_id: "2", parent: "1", name: "two"}
];
into a hierarchical structure. This can be done with a function that adds all children of a given parent p, including, recursively, their children. Like the following:
function children(p) {
var result = [];
for (r of dbresult) if (r.parent === p) {
var row = {_id: r._id, name: r.name};
var chld = children(r._id);
if (chld.length > 0) row.children = chld;
result.push(row);
}
return result;
}
console.log(JSON.stringify(children(null)));
Note that this approach requires only one database access (to fill the dbresult) and is therefore probably faster than your findParent function.

Function Traversing Through An Array of Json Object needs to return one object

So I am not the best at writing functions so I am having a hard time wrapping a head around this. So I am trying to create a function that traverses through array of objects, and stops once it reads the name that is given it must return the number of the that person.
const array = [{name: 'Ann', phone: '575.580.1400', role: 'Developer'},
{name: 'Ben', phone: '575.641.4041', role: 'Manager'},
{name: 'Clara', phone: '512.717.5690', role: 'Developer'}];
const getNumber = (person, book ) => {
for (var x of book ) {
if( x == person) {
return number;}
return ('Not found');
}
}
I know I am missing how to call in the number, but I just can't think of how to do it.
First you need to access the key inside the object and return ('Not found'); is not in the right place. Secondly use === instead of ==.In your code if the function will return in the fist iteration only. Because if you search for Clara and in the if condition Ann will not be equal to Clara so it will return Not Found and will not iterate the remaining array
const array = [{
name: 'Ann',
phone: '575.580.1400',
role: 'Developer'
},
{
name: 'Ben',
phone: '575.641.4041',
role: 'Manager'
},
{
name: 'Clara',
phone: '512.717.5690',
role: 'Developer'
}
];
const getNumber = (person, book) => {
for (var x of book) {
if (x.name === person) {
return x.phone;
}
}
return ('Not found');
}
console.log(getNumber('Clara', array))
Alternatively you can also use array methods like find or filter
const array = [{
name: 'Ann',
phone: '575.580.1400',
role: 'Developer'
},
{
name: 'Ben',
phone: '575.641.4041',
role: 'Manager'
},
{
name: 'Clara',
phone: '512.717.5690',
role: 'Developer'
}
];
const num = array.find(item => item.name === 'Clara').phone;
console.log(num)
Below code helps to iterate through a JSON object and print the required value. You can use the required conditions in IF and print the desired values accordingly.
var json = [{name: 'Ann', phone: '575.580.1400', role: 'Developer'},
{name: 'Ben', phone: '575.641.4041', role: 'Manager'},
{name: 'Clara', phone: '512.717.5690', role: 'Developer'}];
for (var key in json) {
if (json.hasOwnProperty(key)) {
if(json[key].name=='Ben')
console.log(json[key].phone);
}
}
Try this
I am using some instead of foreach or other loops. some() method executes the callback function once for each element present in the array until it finds the one where callback returns a truthy value.
const getNumber = (person, book ) => {
let pnumber;
book.some((item) => {
if(item.name.toLowerCase()===person.toLowerCase()){
pnumber = item.phone;
return true;
}
})
return pnumber ? pnumber: "Not Found" ;
}
you can call it this way
getNumber('clara',array)
"512.717.5690"
getNumber('ben1',array)
"Not Found"

check if data is already present in array of objects

I have the following data with me.
var Inputdata = {};
Inputdata.firstName = 'Raul'
Inputdata.lastName = 'Peters'
I want to check if the firstName and lastName (together) is already present in the array of objects. Can someone please let me know how to achieve this.
UserData: [0]
firstName: 'Raul'
lastName: 'Peters'
Id: '4'
[1]
firstName: 'Amil'
lastName: 'Rikia'
Id: '5'
[2]
firstName: 'Riya'
lastName: 'Pillai'
Id: '6'
[3]
firstName: 'Natasha'
lastName: 'Shacke'
Id: '6'
[4]
firstName: 'Eric'
lastName: 'Coles'
Id: '6'
As you can see, Raul Peters is present in the array of objects in one array. I want the output to be true in this case. If Raul and Peters were in different objects, the answer should be false as Raul and Peters are not in the same object. Can anyone please let me know how to achieve this
Just do an Array.some check:
let isPresent = UserData.some(user => user.firstName == 'Raul' && user.lastName == 'Peters')
Use Array.find
arr.find( function(e){
if(e.firstname == 'Raul' && e.lastname == 'Peters')
return e;
});
const inputData = {};
inputData.firstName = 'Raul';
inputData.lastName = 'Peters';
const userData = [
{
firstName: 'Raul',
lastName: 'Peters',
Id: '4'
},
{
firstName: 'Amil',
lastName: 'Rikia',
Id: '5'
},
{
firstName: 'Riya',
lastName: 'Pillai',
Id: '6'
},
{
firstName: 'Natasha',
lastName: 'Shacke',
Id: '6'
},
{
firstName: 'Eric',
lastName: 'Coles',
Id: '6'
}
];
This function receives 2 arguments: the userData array of objects and the inputData object. Then, it uses the Array.prototype.some() method to see if there is at least one entry within the userData array that is an object matching the inputData object.
function alreadyExists(userData, inputData) {
return userData.some(entry => entry['firstName'] === inputData['firstName'] && entry['lastName'] === inputData['lastName']);
};
console.log(alreadyExists(userData, inputData));
If you wanna use underscore or lodash you can use function _.some. Using internal variable you can get needle element.
var needle = false;
var has = _.some(UserData, function(v) {
return (v.firstName===Inputdata.firstName && v.lastName===Inputdata.lastName) ? needle = v : false;
});
console.log(has)
console.log(needle)

Clean Method to Normalize Javascript Object Properties

I have an array of javascript objects that represent users, like so:
[
{ userName: "Michael",
city: "Boston"
},
{ userName: "Thomas",
state: "California",
phone: "555-5555"
},
{ userName: "Kathrine",
phone: "444-4444"
}
]
Some of the objects contain some properties but not others. What I need is a clean way to ensure ALL objects get the same properties. If they don't exist, I want them to have an empty string value, like so:
[
{ userName: "Michael",
city: "Boston",
state: "",
phone: ""
},
{ userName: "Thomas",
city: "",
state: "California",
phone: "555-5555"
},
{ userName: "Kathrine",
city: "",
state: "",
phone: "444-4444"
}
]
Update
I should have been a little more specific. I was looking for an option that would handle this situation dynamically, so I don't have to know the properties ahead of time.
For jQuery specific, the $.extend() option is a good one, but will only work if you know ALL the properties ahead of time.
A few have mentioned that this should probably be a server-side task, and while I normally agree with that, there are two reasons I'm not handling this at the server-side:
1) it will be a smaller JSON object if say 900 of 1000 objects only contain 1 of a possible 9 properties.
2) the "empty" properties need to be added to satisfy a JS utility that could be replaced in the future with something that doesn't care if some properties are missing.
Since you are using jQuery you can abuse $.extend
function Person(options){
return $.extend({
userName:"",
city: "",
state:"",
phone: ""
},options);
}
$.map([{}],Person)
update
Heres a way to have dynamic default properties
function mapDefaults(arr){
var defaultProperties = {}
for(var i =0; i < arr.length; i++){
$.each(arr[i],function(key){
defaultProperties[key] = "";
});
}
function Defaulter(obj){
return $.extend({},defaultProperties,obj);
}
return $.map(arr, Defaulter);
}
mapDefaults([{a:"valA"},{b:"valB"}]);
/* produces:
[{a:"valA",b:""},{a:"",b:"valB"}]
*/
Something you might try is creating a coalescing function:
function coalesceValues(val){
switch(val)
case undefined:
case null:
return '';
break;
default:
return val;
break;
}
}
Or if you wanted to forego customization for simplicity:
function coalesceValues(val){
return val || '';
}
And then apply it when assigning variables:
var city = coalesceValues(obj.city);
This way you don't need to do any crazy breakdown to array and loop or anything, you can apply it to whatever you want, and you can also customize the values you want to coalesce.
Just offering an alternative idea.
The way that is easiest to understand is probably to make a function that accepts an object and uses if statements as existence checks, assigning a default value if it doesn't find it.
function normalize(object) {
if(typeof object.userName === 'undefined') {
object.userName = 'Default Value';
}
if(typeof object.city === 'undefined') {
object.city = 'Default Value';
}
if(typeof object.state === 'undefined') {
object.state = 'Default Value';
}
if(typeof object.phone === 'undefined') {
object.phone = 'Default Value';
}
return object;
}
var userArray = [{},{},{}].map(normalize);
We can also go the constructor route and provide default values on object creation.
function User (data) {
this.userName = data.userName || 'Default Value';
this.city = data.city || 'Default Value';
this.state = data.state || 'Default Value';
this.phone = data.phone || 'Default Value';
return this;
}
var userArray = [{},{},{}].map(function(o){
return new User(o);
});
Of course this depends on one specific type of data and won't extend to other properties and isn't very DRY, but as I said, this is probably the easiest to understand from a beginner's standpoint.
var list = [
{ userName: "Michael",
city: "Boston"
},
{ userName: "Thomas",
state: "California",
phone: "555-5555"
},
{ userName: "Kathrine",
phone: "444-4444"
}
];
for(var i = 0; i < list.length; i++){
if(list[i].state === undefined)
list[i].state = "";
if(list[i].phone === undefined)
list[i].phone = "";
};
console.log(list);
http://jsfiddle.net/g5XPk/1/
This should probably be a server-side task, but..
If you know all the possible properties ahead of time, you could do this:
http://jsfiddle.net/BMau9/
var properties = ['userName', 'city', 'state', 'phone'];
var data = [{
userName: "Michael",
city: "Boston"
}, {
userName: "Thomas",
state: "California",
phone: "555-5555"
}, {
userName: "Kathrine",
phone: "444-4444"
}];
for (var i in data) {
for (var j in properties) {
data[i][properties[j]] = data[i][properties[j]] || '';
}
}
Fiddle
This function stores unique object keys in an array and so you can run your array of objects through it and then use one of the other supplied answers to add the keys to the objects if they do not exist:
function uniqueKeys(){
var keys=[];
function getUniqueKeys(){
return keys
}
function addObject(obj){
for (var k in obj){
keys = _.union(keys,[k]);
}
}
return {
addObj: addObject,
getKeys: getUniqueKeys
}
}
Usage:
var objArr = [{ userName: "Michael", city: "Boston" },
{ userName: "Thomas", state: "California", phone: "555-5555"},
{ userName: "Kathrine",phone: "444-4444" }];
var uniq = new uniqueKeys();
_.each(objArr, function(v){
uniq.addObj(v)
});
var keys = uniq.getKeys();
alert(keys);
vanilla js
let A = [
{
userName: "Michael",
city: "Boston",
},
{
userName: "Thomas",
state: "California",
phone: "555-5555",
},
{
userName: "Kathrine",
phone: "444-4444",
},
];
// set-difference
const diff = (a,b) => new Set([...a].filter((x) => !b.has(x)));
// all keys
const K = new Set(arr.map(o => Object.keys(o)).flat());
// add missing keys and default vals
A.forEach((e,i) => diff(K, new Set(Object.keys(e))).forEach(k => A[i][k] = ""));

Underscore contains based on object property

I would like to use Underscore.js to determine if an instance of an object is present in an array.
An example usage would be:
var enrollments = [
{ userid: 123, courseid: 456, enrollmentid: 1 },
{ userid: 123, courseid: 456, enrollmentid: 2 },
{ userid: 921, courseid: 621, enrollmentid: 3 }
]
I want to be able to identify unique an enrollment where the userid and courseid are the same.
So basically, given a list of enrollments I can remove duplicates based on matches to the userid and courseid, but not the enrollment id.
You can use the filter method from Underscore:
function contains(arr, userid, courseid){
var matches = _.filter(arr, function(value){
if (value.userid == userid && value.courseid == courseid){
return value;
}
});
return matches;
}
contains(enrollments, 123, 456);

Categories

Resources