Replace some objects and arrays using javascript? - javascript

I am using nodejs as server side, i got some of the json objects
This is my json array with objects
[
{
"id": 20,
"gsm": "123456789",
"firstName": "Mohamed",
"lastName": "Sameer",
"contactgroups": [
{
"contactId": 20,
"groupId": 14,
"group": {
"groupname": "Angular"
}
}
]
},
{
"id": 21,
"gsm": "987654321",
"firstName": "Ganesh",
"lastName": "Pandiyan",
"contactgroups": [
{
"contactId": 21,
"groupId": 14,
"group": {
"groupname": "Angular"
}
},
{
"contactId": 21,
"groupId": 15,
"group": {
"groupname": "React"
}
}
]
}
]
I want final output like this(See my groupname key):
[
{
"id": 20,
"gsm": "123456789",
"firstName": "Mohamed",
"lastName": "Sameer",
"contactgroups": [
{
"contactId": 20,
"groupId": 14,
"groupname": "Angular",
"group": {}
}
]
},
{
"id": 21,
"gsm": "987654321",
"firstName": "Ganesh",
"lastName": "Pandiyan",
"contactgroups": [
{
"contactId": 21,
"groupId": 14,
"groupname": "Angular",
"group": {}
},
{
"contactId": 21,
"groupId": 15,
"groupname": "React",
"group": {}
}
]
}
]
is it possible? to do, which is best map or reduce or lodash? any method?
I want to change the place of groupname and to remove the groupname from group object.

You can use array#map. This will return a new array with modified keys & values
var orgArray = [{
"id": 20,
"gsm": "123456789",
"firstName": "Mohamed",
"lastName": "Sameer",
"contactgroups": [{
"contactId": 20,
"groupId": 14,
"group": {
"groupname": "Angular"
}
}]
},
{
"id": 21,
"gsm": "987654321",
"firstName": "Ganesh",
"lastName": "Pandiyan",
"contactgroups": [{
"contactId": 21,
"groupId": 14,
"group": {
"groupname": "Angular"
}
},
{
"contactId": 21,
"groupId": 15,
"group": {
"groupname": "React"
}
}
]
}
]
var newArray = orgArray.map(function(item) {
return {
"id": item.id,
"gsm": item.gsm,
"firstName": item.firstName,
"lastName": item.lastName,
// an IIFE which will create the new contactgroups
"contactgroups": (function() {
return item.contactgroups.map(function(item2, index) {
return {
"contactId": item2.contactId,
"groupId": item2.groupId,
"groupname": item2.group.groupname,
"group": {}
}
})
}())
}
})
console.log(newArray);

Related

Fetch object id with Javascript

I have the following array of objects:
[
{
"id": 97,
"name": "Jon",
"technologies": [
{
"id": 6,
"name": "React"
},
{
"id": 7,
"name": "Messageria"
}
]
},
{
"id": 98,
"name": "Doe",
"technologies": [
{
"id": 2,
"name": "Javascript"
},
{
"id": 6,
"name": "React"
}
]
},
{
"id": 99,
"name": "Mark",
"technologies": [
{
"id": 8,
"name": "PHP"
},
{
"id": 9,
"name": "Laravel"
}
]
}
]
How could I filter this array and return only developers who have, for example, technology with id 6?
The return I need is only developers who have a relationship with technology id 6, however I need other related technologies to also appear to the developer.
I know that through the find method it is possible to do this, but I don't know how to implement it.
const result = developers.find(dev => dev.technologies ?);
What would be the correct form?
You can use filter with some array operations for this task. Something like this:
persons.filter(person => person.technologies.some(tech => tech.id == 6))
This will return details of person with technology id 6:
persons = [
{
"id": 97,
"name": "Jon",
"technologies": [
{
"id": 6,
"name": "React"
},
{
"id": 7,
"name": "Messageria"
}
]
},
{
"id": 98,
"name": "Doe",
"technologies": [
{
"id": 2,
"name": "Javascript"
},
{
"id": 6,
"name": "React"
}
]
},
{
"id": 99,
"name": "Mark",
"technologies": [
{
"id": 8,
"name": "PHP"
},
{
"id": 9,
"name": "Laravel"
}
]
}
]
persons.filter(person => person.technologies.find(tech => tech.id == 6))
You need to use some for finding whether element exist with specific id or not:
const data = [
{
"id": 97,
"name": "Jon",
"technologies": [
{
"id": 6,
"name": "React"
},
{
"id": 7,
"name": "Messageria"
}
]
},
{
"id": 98,
"name": "Doe",
"technologies": [
{
"id": 2,
"name": "Javascript"
},
{
"id": 6,
"name": "React"
}
]
},
{
"id": 99,
"name": "Mark",
"technologies": [
{
"id": 8,
"name": "PHP"
},
{
"id": 9,
"name": "Laravel"
}
]
}
]
const myFilteredData = data.filter(test => test.technologies.some(data => data.id===6));
console.log(
myFilteredData
)

Filter Array of user objects by array of roles in Javascript

I have an array of user objects.
I want to filter them based on the array of user roles.
filter = ['ROLE_SELLER', 'ROLE_BANK', 'ROLE_CPF', 'ROLE_SLA', 'ROLE_LDAU']
const users = [{
"id": 1,
"email": "user1#test.com",
"name": "User1",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 2,
"email": "user2#test.com",
"name": "User2",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
}]
}, {
"id": 3,
"email": "user3#test.com",
"name": "User3",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
}]
}, {
"id": 4,
"email": "user4#test.com",
"name": "User4",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 5,
"email": "user5#test.com",
"name": "User5",
"roles": [{
"id": 5,
"code": "ROLE_LAWYER",
"name": "Lawyer"
}]
}, {
"id": 6,
"email": "user6#test.com",
"name": "User6",
"roles": [{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}]
},
{
"id": 7,
"email": "user7#test.com",
"name": "User7",
"roles": [{
"id": 9,
"code": "ROLE_SLA",
"name": "sla"
}]
},
{
"id": 8,
"email": "user8#test.com",
"name": "User8",
"roles": [{
"id": 8,
"code": "ROLE_BANK",
"name": "Bank"
}]
},
{
"id": 9,
"email": "user9#test.com",
"name": "User9",
"roles": [{
"id": 7,
"code": "ROLE_CPF",
"name": "CPF"
}]
}
]
const filter = ['ROLE_SELLER', 'ROLE_BANK', 'ROLE_CPF', 'ROLE_SLA', 'ROLE_LDAU']
const filteredUsers = users.filter(user => !user.roles.find(role => filter.includes(role.id)))
console.log(filteredUsers)
Expected result
[{
"id": 1,
"email": "user1#test.com",
"name": "User1",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 4,
"email": "user4#test.com",
"name": "User4",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 6,
"email": "user6#test.com",
"name": "User6",
"roles": [{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}]
},
{
"id": 7,
"email": "user7#test.com",
"name": "User7",
"roles": [{
"id": 9,
"code": "ROLE_SLA",
"name": "sla"
}]
},
{
"id": 8,
"email": "user8#test.com",
"name": "User8",
"roles": [{
"id": 8,
"code": "ROLE_BANK",
"name": "Bank"
}]
},
{
"id": 9,
"email": "user9#test.com",
"name": "User9",
"roles": [{
"id": 7,
"code": "ROLE_CPF",
"name": "CPF"
}]
}
]
I am trying to image what result you want to receive....
You want to filter of users by array with possible roles, another worlds if user has one of roles of filter array you want to pass his to 'filteredUsers' array?
filter.includes(role.id) - I guess it is wrong, may be you want to
filter by role.code?
Array.find() doesn't support Explorer
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
Array.includes() - doesn't support Explorer too.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
I think the reason why that was happening was that you are trying to match the role id with the 'filter' array which contains the role code. From what i observed from the expected answer, you are looking for user who has any one of their role fits the filter. So do the following will filter by role code and with the expected result
const filteredUsers = users.filter((user) => {
return user.roles.map(role=>filter.includes(role.code)).includes(true)
})
this line of code basically map the filter to every role object to every user, if their role code is included in the filter it will add a true to the array(return array of map()) and for the filter function if the map() return array contains true then true(so basically a || for the whole array)

Filter search based on array values in angular 4

I am new in angular 4. While trying to make a search bar got stuck in the logic. So I have a JSON file, and I am trying to search relevant key value pair of that JSON file based on an array values.
My json file :
const trips = {
"20180201": [{
"journeyId": 1001,
"Number": "001",
"DriverName": "Alex",
"Transporter": {
"id": "T1",
"number": "AN01001",
"Company": "Tranzient"
},
"place": [{
"id": 001,
"value": "Washington DC"
},
{
"id": 002,
"value": "Canberra"
}
],
},
{
"journeyId": 1002,
"Number": "001",
"DriverName": "Tom",
"Transporter": {
"id": "T2",
"number": "AN01002",
"Company": "Trax"
},
"place": [{
"id": 2,
"value": "Canberra"
},
{
"id": 4,
"value": "Vienna"
}
],
},
{
"journeyId": 1003,
"Number": "004",
"DriverName": "Jack",
"Transporter": {
"id": "T3",
"number": "AN01003",
"Company": "Trax"
},
"place": [{
"id": 1,
"value": "Washington DC",
}, {
"id": 4,
"value": "Vienna",
}],
}
],
"20180211": [{
"journeyId": 1004,
"Number": "005",
"DriverName": "Jack",
"Transporter": {
"id": "T3",
"number": "AN01013",
"Company": "Trax"
},
"place": [{
"id": 5,
"value": "Bridgetown"
},
{
"id": 6,
"value": "Ottawa"
},
{
"id": 4,
"value": "Boston"
}
],
},
{
"journeyId": 1005,
"Number": "005",
"DriverName": "Jerry",
"Transporter": {
"id": "T3",
"number": "AN01020",
"Company": "Trax"
},
"place": [{
"id": 5,
"value": "Bridgetown"
},
{
"id": 6,
"value": "Ottawa"
}
],
}
],
"20180301": [{
"journeyId": 1006,
"Number": "005",
"DriverName": "demy",
"Transporter": {
"id": "T3",
"number": "AN01003",
"Company": "Trax"
},
"place": [{
"id": 5,
"value": "Bridgetown"
},
{
"id": 6,
"value": "Vienna"
}
],
}],
};
I have an array
SelectedValues= ["20180201","Vienna"]
All the array values should search depending on the result from its previous array value. For example in this array above, I need to return key value pair of all the places which have its value "Vienna" in date "20180201".
expected output:
trips= {
"20180201": [
{
"journeyId": 1002,
"Number": "001",
"DriverName": "Tom",
"Transporter": {
"id": "T2",
"number": "AN01002",
"Company": "Trax"
},
"place": [{
"id": 2,
"value": "Canberra"
},
{
"id": 4,
"value": "Vienna"
}
],
},
{
"journeyId": 1003,
"Number": "004",
"DriverName": "Jack",
"Transporter": {
"id": "T3",
"number": "AN01003",
"Company": "Trax"
},
"place": [{
"id": 1,
"value": "Washington DC",
}, {
"id": 4,
"value": "Vienna",
}],
}
]
}
So the result returns all the key value pair that have place "Vienna" within the date " 20180201 " .
My solution that I tried to use :
filter(trips, SelectedValues) {
const check = v => options.includes(v) || v && typeof v === 'object' && Object.keys(v).some(l => check(v[l]));
var result = {};
Object.keys(object).forEach(function (k) {
var temp = options.includes(k)
? object[k]
: object[k].filter(check);
if (temp.length) {
result[k] = temp;
}
});
return result;
}
The problem is that it is returning all the key-value pairs that matches all the values in SelectedValues(Implementing OR logic). But I need to implement the array search with AND logic.

Using angularjs forEach loops

I am getting this type of json in my $scope of angularjs:
$scope.someStuff = {
"id": 2,
"service": "bike",
"min": "22",
"per": "100",
"tax": "1",
"categoryservices": [
{
"id": 32,
"category": {
"id": 1,
"name": "software"
}
},
{
"id": 33,
"category": {
"id": 2,
"name": "hardware"
}
},
{
"id": 34,
"category": {
"id": 3,
"name": "waterwash"
}
}
]
}
I want to use angularjs forEach loop and i want to get only category name,
My expected output:
[{"name":"software"}, {"name":"hardware"}, {"name":"waterwash"}]
You can use Array.map()
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
$scope.someStuff.categoryservices.map((x) => { return { name: x.category.name}})
var obj = {
"id": 2,
"service": "bike",
"min": "22",
"per": "100",
"tax": "1",
"categoryservices": [{
"id": 32,
"category": {
"id": 1,
"name": "software"
}
},
{
"id": 33,
"category": {
"id": 2,
"name": "hardware"
}
},
{
"id": 34,
"category": {
"id": 3,
"name": "waterwash"
}
}
]
};
console.log(obj.categoryservices.map((x) => {
return {
name: x.category.name
}
}))
You can use map method by passing a callback function as parameter.
const someStuff = { "id": 2, "service": "bike", "min": "22", "per": "100", "tax": "1", "categoryservices": [ { "id": 32, "category": { "id": 1, "name": "software" } }, { "id": 33, "category": { "id": 2, "name": "hardware" } }, { "id": 34, "category": { "id": 3, "name": "waterwash" } } ] }
let array = someStuff.categoryservices.map(function({category}){
return {'name' : category.name}
});
console.log(array);

Using map to change arrays in Javascript

I have this type of json object:
{
"id": 12,
"firstName": "Mohamed",
"lastName": "Sameer",
"contactgroups": [
{
"id": 16,
"group": {
"id": 4,
"groupname": "Angular"
}
},
{
"id": 19,
"group": {
"id": 5,
"groupname": "React"
}
},
{
"id": 20,
"group": {
"id": 6,
"groupname": "Node"
}
}
]
}
I want output like this:
{
"id": 12,
"firstName": "Mohamed",
"lastName": "Sameer",
"groups": [4,5,6] // coming from group object which has id and groupname //
}
How to do using any of the javascript methods, i dont need for loops.
How to do using map?
I tried this:
var data = {
"id": 12,
"firstName": "Mohamed",
"lastName": "Sameer",
"contactgroups": [
{
"id": 16,
"group": {
"id": 4,
"groupname": "Angular"
}
},
{
"id": 19,
"group": {
"id": 5,
"groupname": "React"
}
},
{
"id": 20,
"group": {
"id": 6,
"groupname": "Node"
}
}
]
}
var finalData = data.contactgroups.map(x=> {
return ({
id : data.id,
firstName: data.firstName,
lastName: data.lastName,
groups: [x.group.id]
})
})
console.log(finalData);
Try something like this. BTW, I used ES6 syntax.
let obj1 = {
"id": 12,
"firstName": "Mohamed",
"lastName": "Sameer",
"contactgroups": [
{
"id": 16,
"group": {
"id": 4,
"groupname": "Angular"
}
},
{
"id": 19,
"group": {
"id": 5,
"groupname": "React"
}
},
{
"id": 20,
"group": {
"id": 6,
"groupname": "Node"
}
}
]
};
let groups = obj1.contactgroups.map(item=>(item.group.id));
let obj2 = {
"id": obj1.id,
"firstName": obj1.firstName,
"lastName": obj1.lastName,
"groups": groups
}
Take some destructuring assignments and return a new object with mapped id.
function getGrouped({ id, firstName, lastName, contactgroups: groups }) {
return { id, firstName, lastName, groups: groups.map(({ group: { id } }) => id) };
}
var data = { id: 12, firstName: "Mohamed", lastName: "Sameer", contactgroups: [{ id: 16, group: { id: 4, groupname: "Angular" } }, { id: 19, group: { id: 5, groupname: "React" } }, { id: 20, group: { id: 6, groupname: "Node" } }] };
console.log(getGrouped(data));
the answer to your question would be the below :
var finalData = {
id : data.id,
firstName: data.firstName,
lastName: data.lastName,groups: data.contactgroups.map(x=> x.group.id)}
console.log(finalData);

Categories

Resources