I have data in format like this:-
a.arr= [
0:{
name: abc,
dob: 18/12/1917,
panNo: ASDFG6789K
},
1:{
name: xyz,
dob: 1/2/1917,
panNo: WERTY6789K
}
]
I want to remove 0 and 1 and convert it in like this:-
a.arr= [
{
name: abc,
dob: 18/12/2017,
panNo: ASDFG6789K
},
{
name: xyz,
dob: 1/2/1917,
panNo: WERTY6789K
}
]
If I use delete then it removes the data as well inside object. Is there any other way to do this?
As some of the previous commenters said, your first code snippet does not contain valid JavaScript. However, if both the 0 and 1 become strings, as well as all of the values (abc, 18/12/1917, etc.), your data would look like this:
var a = {};
a.arr = [
{
"0": {
name: "abc",
dob: "18/12/1917",
panNo: "ASDFG6789K"
}
},
{
"1": {
name: "xyz",
dob: "1/2/1917",
panNo: "WERTY6789K"
}
}
];
To remove the 0 and 1, you could then do this:
var arr = a.arr.map(function(d){
var key = Object.keys(d)[0];
return d[key];
});
a.arr = arr;
// a.arr is now:
[{
"name": "abc",
"dob": "18/12/1917",
"panNo": "ASDFG6789K"
}, {
"name": "xyz",
"dob": "1/2/1917",
"panNo": "WERTY6789K"
}]
Related
I need help with a JavaScript task.
How do I get something specific queried from two databases?
Task:
View the index.js file
Implement the gradeOverview() function, which gets the variables students and
grades and creates a grade overview for each student. Thereby
each element in the students array should be projected to an object in the following format:
{ student: (students[i]), grades: [(grades[j], grades[j+k], ...)] }
This is my function which accesses both databases and should retrieve and display a value from one database at a time based on the "student number".
function gradeOverview(students, grades) {
const result = students.map((student) => [
{
student: student,
grade: grades.reduce((grades, grade) => {
const student number = grade.studentnumber;
if (grades[matriculationnumber] == null) grades[matriculationnumber] = [];
grades[matriculationnumber].push(grade);
return grades;
}),
},
]);
console.log(result);
return result;
// TODO: implement me
}
The Data:
var students = [{
"matrikelnummer": 4636,
"vorname": "Vérane",
"nachname": "Voase"
}]
var grades = [{
"id": 628,
"matrikelnummer": 4636,
"grade": "3,3"
},
{
"id": 886,
"matrikelnummer": 4636,
"grade": "5,0"
}]
Output:
"student": {
"matrikelnummer": 4636,
"vorname": "Vérane",
"nachname": "Voase"
},
"grades": [
{
"id": 628,
"matrikelnummer": 4636,
"grade": "3,3"
},
{
"id": 886,
"matrikelnummer": 4636
"grade": "3,6"
}
]
},
Updated: Fiddle Link https://jsfiddle.net/uzrodex3/
I hope i guessed correctly your incoming data you put into gradeOverview function.
const studentsQueryResponse = [
{
name: "John Doe",
studentNumber: 123
},
{
name: "Johan Doe",
studentNumber: 321
},
{
name: "Jane Doe",
studentNumber: 111
}
]
const gradesQueryResponse = [
{
studentNumber: 123,
grade: 1
},
{
studentNumber: 123,
grade: 3
},
{
studentNumber: 321,
grade: 5
},
{
studentNumber: 123,
grade: 1
}
]
There is your function which returns in this case
[
{
"student": {
"name":"John Doe",
"studentNumber":123
},
"grade":[1,3,1]
},
{
"student": {
"name":"Johan Doe",
"studentNumber":321
},
"grade":[5]
},
{
"student": {
"name":"Jane Doe",
"studentNumber":111
},
"grade":[]
}
]
If you want to return whole grade object in array, then just remove map line which returns grade property value from grade object
function gradeOverview(students, grades) {
console.log("Input arguments", students, grades)
const results = students.map((student) => {
return {
student: student,
grade: grades
.filter((gradeObject) => gradeObject.studentNumber === student.studentNumber)
.map((gradeObject) => gradeObject.grade)
}
});
console.log("Results", results);
return results;
}
And then you call your function
gradeOverview(studenstQueryResponse, gradesQueryResponse)
Here is my objects
{"seatName": seats[i].name,
"fare": seats[i].fare,
"passenger": [{
"name":"",
"gender": "",
// "email":this.props.authStore.email,
// "mobile": this.props.authStore.phone
}]
}
Here, i need to include
"email":this.props.authStore.email,
"mobile": this.props.authStore.phone
When (i === 0), other than 0, for other these value should not be included ! How to achieve this ?
I tried like this :
(i === 0) ? "email": (i === 0) ? this.state.primaryUserEmail : "",
But it throws error !
Try using this :
...(i === 0 && {email: this.props.authStore.email}),
It will include when conditions satisfied and removes when not !
let data = {"seatName": "Joe",
"fare": 12,
"passenger": [{
"name":"",
"gender": "",
// "email":this.props.authStore.email,
// "mobile": this.props.authStore.phone
}]
}
/*
you can simply check if that passenger object has an email and mobile number and if they don't have you can then assign the number and email.
*/
if(!data.passenger[0]["email"]){
data.passenger[0]["email"] = "test#email.com";
}
if(!data.passenger[0]["mobile"]){
data.passenger[0]["mobile"] = 1234567890;
}
console.log(data)
/*
OUTPUT:
{
seatName: 'Joe',
fare: 12,
passenger: [ { name: '', gender: '', email: "test.email.com", mobile: 1234567890 } ]
}
*/
i have two objects like this
languages = [
{
"name": "english",
"iso_639_2_code": "eng"
},
{
"name": "esperanto",
"iso_639_2_code": "epo"
},
{
"name": "estonian",
"iso_639_2_code": "est"
}
]
and another is
user = [
{
name: "john",
language: "eng",
country: "US"
}
];
what i have to do is, match iso_639_2_code to language of user then, i have to display Language name not code from languages. basically both are different api, and i have no idea how to do it this in angular 4.
here's a link what i am trying https://stackblitz.com/edit/angular-9k2nff?file=app%2Fapp.component.ts
Use array find:
var languages = [
{"name": "english", "iso_639_2_code": "eng"},
{"name": "esperanto","iso_639_2_code": "epo"},
{"name": "estonian","iso_639_2_code": "est"}
];
var user = [{name: "john",language: "eng",country: "US"}];
var language = languages.find(l => l.iso_639_2_code === user[0].language);
var languageName = language && language.name; // <-- also prevent error when there is no corresponding language found
console.log(languageName);
EDIT:
With multiple user, it will be:
var languages = [
{"name": "english", "iso_639_2_code": "eng"},
{"name": "esperanto","iso_639_2_code": "epo"},
{"name": "estonian","iso_639_2_code": "est"}
];
var users = [
{name: "john",language: "eng",country: "US"},
{name: "john",language: "epo",country: "Esperanto"}
];
var languageNames = languages.filter(
l => users.find(u => l.iso_639_2_code === u.language)
).map(lang => lang.name);
console.log(languageNames);
Use find
var output = languages.find(s => s.iso_639_2_code == user[0].language).name;
Demo
var languages = [{
"name": "english",
"iso_639_2_code": "eng"
},
{
"name": "esperanto",
"iso_639_2_code": "epo"
},
{
"name": "estonian",
"iso_639_2_code": "est"
}
];
var user = [{
name: "john",
language: "eng",
country: "US"
}
];
var output = languages.find(s => s.iso_639_2_code == user[0].language).name;
console.log(output);
Or, if there are multiple users, and you want to find language name for each of them, then use map
var output = user.map(t =>
languages.find(s =>
s.iso_639_2_code == t.language).name);
Demo
var languages = [{
"name": "english",
"iso_639_2_code": "eng"
},
{
"name": "esperanto",
"iso_639_2_code": "epo"
},
{
"name": "estonian",
"iso_639_2_code": "est"
}
];
var user = [{
name: "john",
language: "eng",
country: "US"
}
];
var output = user.map(t =>
languages.find(s =>
s.iso_639_2_code == t.language).name);
console.log(output);
I think here is what you need , for output just run the snippet :
var languages = [
{
"name": "english",
"iso_639_2_code": "eng"
},
{
"name": "esperanto",
"iso_639_2_code": "epo"
},
{
"name": "estonian",
"iso_639_2_code": "est"
}
];
var user = [
{
name: "john",
language: "eng",
country: "US"
}
];
user.map(u => {
let flang = languages.filter(lang => lang.iso_639_2_code === u.language);
if(flang) {
u.language = flang[0].name;
}
return u;
})
console.log(user);
var languages=[
{"name":"english","iso_639_2_code":"eng"},
{"name":"esperanto","iso_639_2_code":"epo"},
{"name":"estonian","iso_639_2_code":"est"}
];
var user=[
{name:"john",language:"eng",country:"US"}
];
var languageFound = languages.find(lang => lang.iso_639_2_code === user[0].language);
if(languageFound){
var languageName = languageFound.name;
console.log(languageName);
}
I have got the following array of Usernames
Usernames = [
{
"id": 1,
"userName": "Jack",
"description": "jack is a nice guy",
"userRoleIds": [
1
]
},
{
"id": 2,
"userName": "Caroline",
"description": "Good girl",
"userRoleIds": [
2,3
]
},
{
"id": 3,
"userName": "Smith",
"description": "Smithyyyy",
"userRoleIds": [
1,2
]
}
]
And an array of userRoles.
userRoles = [
{
id: 1,
roleName: "Admin"
},
{
id: 2,
roleName: "Tester"
},
{
id: 3,
roleName: "Developer"
}
]
What i want to get done is first concat the arrays in in Usernames and userRoles to get the following result.
Usernames = [
{
"id": 1,
"userName": "Jack",
"description": "jack is a nice guy",
"userRoleIds": [
{
"id": 1,
"roleName" : "Admin"
}
]
},
{
"id": 2,
"userName": "Caroline",
"description": "Good girl",
"userRoleIds": [
{
"id": 2,
"roleName" : "Tester"
},
{
"id": 3,
"roleName" : "Developer"
}
]
},...
The second thing i want is to be able to filter for the roleName and userName seperated by pipe signs. As in type something in a text box that searches for userName and roleName for example.
if i type
Caroline, Tester
The result will be
result = [
{
"id": 2,
"userName": "Caroline",
"description": "Good girl",
"userRoleIds": [
2,3
]
},
{
"id": 3,
"userName": "Smith",
"description": "Smithyyyy",
"userRoleIds": [
1,2
]
}
]
What is the best practice for achieving this?
Thanks
Here is how I would do it. I prefer using services and take advantage of their functions to keep code clean.
app.service('UserService', function (PermisionsServices) {
var self = {
'list': [],
'load': function (Users) {//Pass your array of Users
angular.forEach(Users, function (user) {
angular.forEach(user.userRoleIds, function (role) {
self.user.userRolesIds.push(PermisionsServices.get(role));
});
self.list.push(user);
});
}, 'get': function (id) {
for (var i = 0; i < self.list.length; i++) {
var obj = self.list[i];
if (obj.id == id) {
return obj;
}
}
}
};
return self;
});
app.service('PermisionsServices', function () {
var self = {
'list': [],
'load': function (permisions) {//Pass your array of permisions
angular.forEach(permisions, function (permision) {
self.list.push(permision);
});
}, 'get': function (id) {
for (var i = 0; i < self.list.length; i++) {
var obj = self.list[i];
if (obj.id == id) {
return obj;
}
}
}
};
return self;
});
Afterwards, you can use it on your controller:
$scope.users=UserService;
And access each of the users as a separate object which can have multiple object permisions.
NOTE: Building the service (populating it) will of course depend on your app logic and controller, you could just easily remove the "load" function and just hardcode the list object by copy and pasting your arrays.
This is the approach I use to load data from API via resource.
Regards
Edit:
For use on the UI, you would just call:
<div ng-repeat='user in users.list'>
{{user.name}} has {{user.permissions}}
</div>
as the object information is already contained within it.
Edit 2:
If you want to search your data, then you can just add a filter like this:
<div ng-repeat='user in users.list | filter: filterList'>
{{user.name}} has {{user.permissions}}
</div>
And then on the controller:
$scope.filterList = function (user) {
if ($scope.filterTextBox) {
return user.name.indexOf($scope.filterTextBox) == 0;
}
return true;
}
Hope this works for you
I would do with pure JS like this. It won't take more than a single assignment line each.
var Usernames = [
{
"id": 1,
"userName": "Jack",
"description": "jack is a nice guy",
"userRoleIds": [
1
]
},
{
"id": 2,
"userName": "Caroline",
"description": "Good girl",
"userRoleIds": [
2,3
]
},
{
"id": 3,
"userName": "Smith",
"description": "Smithyyyy",
"userRoleIds": [
1,2
]
}
],
userRoles = [
{
id: 1,
roleName: "Admin"
},
{
id: 2,
roleName: "Tester"
},
{
id: 3,
roleName: "Developer"
}
],
modified = Usernames.reduce((p,c) => (c.userRoleIds = c.userRoleIds.map(e => e = userRoles.find(f => f.id == e)),p.concat(c)),[]),
query = ["Caroline","Tester"],
filtered = modified.filter(f => query.includes(f.userName) || f.userRoleIds.some(e => query.includes(e.roleName)));
console.log(JSON.stringify(modified,null,2));
console.log(JSON.stringify(filtered,null,2));
You can use lodash to achieve this.
var role = _.find(userRoles, function(role) {
return role.roleName == 'Tester';
});
_.find(Usernames, function(user) {
return user.userName == 'Caroline' || _.indexOf(user.userRoleIds, role.id)>=0;
});
Using the aggregate pipeline, I am trying to project an embedded document to the root level WITHOUT projecting each field individually.
For example, I want to project name from this collection to the root level:
[
{
_id: "1",
name: {
firstName: "John",
lastname: "Peters"
}
},
{
_id: "2",
name: {
firstName: "Mary",
lastname: "Jones"
}
}
]
This is what I am looking for:
[
{
firstName: "John",
lastname: "Peters"
},
{
firstName: "Mary",
lastname: "Jones"
}
]
Is there a way to do this without projecting each field individually? I don't want to have to do this:
db.collection.aggregate(
[
{
$project : {
"_id" : 0,
"firstName" : "$name.firstName",
"lastName" : "$name.lastName"
}
}
]
MongoDB 3.4 has the new stage in aggregation pipeline - $replaceRoot, which does exactly what was asked.
https://docs.mongodb.com/manual/reference/operator/aggregation/replaceRoot/
Here is the solution which uses JavaScript variable.
# Set Object for what to project
var projectWhat = {'_id' : 0};
# Fill Object with keys
Object.keys(db.coll.findOne().name).forEach(function(x){
projectWhat[x] = "$name." + x;
});
# Do Aggregate
db.coll.aggregate([{$project : projectWhat}])
And the output will be
{ "firstName" : "John", "lastname" : "Peters" }
{ "firstName" : "Mary", "lastname" : "Jones" }
Hope this helps.
You can use $replaceRoot like this:
db.collection.aggregate(
[
{
$replaceRoot : {
newRoot: {"$name"}
}
}
]
)
Also if you have a field in the root document you want to retain you can use a $mergeObjects to combine it with your embedded object:
db.collection.aggregate(
[
{
$replaceRoot : {
newRoot: {
$mergeObjects: [
{"_id": "$_id"},
"$name"
]
}
}
}
]
)
This may be achieved by using $set to update all documents with the values in the name sub-document:
db.collection.find({ "name": {"$exists": 1 } }).forEach(function(doc) {
var setName = {};
for ( var k in doc.name ) {
setName[k] = doc.name[k];
}
db.collection.update(
{ "_id": doc._id },
{ "$set": setName, "$unset": "name" }
);
})
While I'll recommend you use $project because it would be more performant than this solution, I can understand why you wouldn't want to use $project.
Starting Mongo 4.2, the $replaceWith aggregation operator can be used to replace a document by another (in our case by a sub-document):
// { _id: "1", name: { firstName: "John", lastname: "Peters" } }
// { _id: "2", name: { firstName: "Mary", lastname: "Jones" } }
db.collection.aggregate({ $replaceWith: "$name" })
// { firstName: "John", lastname: "Peters" }
// { firstName: "Mary", lastname: "Jones" }