How to combine 2 arrays in Javascript with headers - javascript

So I have 2 arrays:
(from user table)
[{
"id": "123",
"name": "Peter",
"description": "person in the office"
}]
(from department table)
[{
"id": "R1",
"name": "Marketing",
"description": "Yea Marketing"
},
{
"id": "R2",
"name": "Sales",
"description": "More Sales"
}]
what I want to do is combine the two arrays into a single array to look like this:
[{
"user": [{
"id": "123",
"name": "Peter",
"description": "person in the office" }],
"department": [{
"id": "R1",
"name": "Marketing",
"description": "Yea Marketing"
},
{
"id": "R2",
"name": "Sales",
"description": "More Sales"
}]
}]
I know how to concat arrays and push but both ways only merge the data together without allowing me to add in the headers of user and department. If I use concat with strings to add in the headers then I get back [{ "\"user\": [{ \n \"name\":.....
Is there a way I can combine the arrays and add some type of header field?

Is this what you're after?
var user = [{
"id": "123",
"name": "Peter",
"description": "person in the office"
}];
var departments = [{
"id": "R1",
"name": "Marketing",
"description": "Yea Marketing"
},
{
"id": "R2",
"name": "Sales",
"description": "More Sales"
}];
var combined = [{
user: user,
department: departments,
}];

var userArr = [{
"id": "123",
"name": "Peter",
"description": "person in the office"
}];
var deptArr = [{
"id": "R1",
"name": "Marketing",
"description": "Yea Marketing"
},
{
"id": "R2",
"name": "Sales",
"description": "More Sales"
}];
var merged = [];
merged.push({'user':userArr,'department':deptArr});

Related

How to delete all properties of a type JSON [duplicate]

This question already has answers here:
Remove property for all objects in array
(18 answers)
Closed 1 year ago.
I have the following object in my code.
[
{
"Name": "John Johnsson",
"Adress": "Linkoping",
"Id": 0,
"Age": "43",
"Role": "Software Engineer"
},
{
"Name": "Marcus Svensson",
"Adress": "Norrköping",
"Age": "26",
"Id": 1,
"Role": "Project Manager"
},
{
"Age": "25",
"Name": "Trevor McNoah",
"Id": 2,
"Adress": "Stockholm",
"Role": "CTO"
}
]
How do I best delete all the "Adress" fields? So I end up with the following result. I've been struggling to find an answer to this basic question.
[
{
"Name": "John Johnsson",
"Id": 0,
"Age": "43",
"Role": "Software Engineer"
},
{
"Name": "Marcus Svensson",
"Age": "26",
"Id": 1,
"Role": "Project Manager"
},
{
"Age": "25",
"Name": "Trevor McNoah",
"Id": 2,
"Role": "CTO"
}
]
JavaScript has delete operator:
data.forEach(item => {
delete item['Address'];
})
You can read more about delete operator here.
You can do it like that: so you don't mutate the initial array.
const listWithoutAddress = list.map(({Adress, ...rest}) => ({...rest}));
To remove a property from a js object you can use the delete command, like this:
let arr = [
{
"Name": "John Johnsson",
"Address": "Linkoping",
"Id": 0,
"Age": "43",
"Role": "Software Engineer"
},
{
"Name": "Marcus Svensson",
"Address": "Norrköping",
"Age": "26",
"Id": 1,
"Role": "Project Manager"
},
{
"Age": "25",
"Name": "Trevor McNoah",
"Id": 2,
"Address": "Stockholm",
"Role": "CTO"
}
];
console.log(arr);
arr.forEach(_=>{delete _.Address}); // <-- this is it
console.log(arr);

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)

How can I access specific value of JSON object? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I can't seem to console.log the item name and just that. It's in the position data -> pricing -> tables -> items -> name. I am going for an output that says "Toy Panda".
[
{
"event": "recipient_completed",
"data": {
"id": "msFYActMfJHqNTKH8YSvF1",
"name": "Sample Document",
"status": "document.draft",
"date_created": "2014-10-06T08:42:13.836022Z",
"date_modified": "2016-03-04T02:21:13.963750Z",
"action_date": "2016-09-02T22:26:52.227554",
"action_by": {
"id": "FyXaS4SlT2FY7uLPqKD9f2",
"email": "john#appleseed.com",
"first_name": "John",
"last_name": "Appleseed"
},
"created_by": {
"id": "FyXaS4SlT2FY7uLPqKD9f2",
"email": "john#appleseed.com",
"first_name": "John",
"last_name": "Appleseed",
"avatar": "https://pd-live-media.s3.amazonaws.com/users/FyXaS4SlT2FY7uLPqKD9f2/avatar.jpg"
},
"recipients": [
{
"id": "FyXaS4SlT2FY7uLPqKD9f2",
"email": "john#appleseed.com",
"first_name": "John",
"last_name": "Appleseed",
"role": "signer",
"recipient_type": "Signer",
"has_completed": true
}
],
"sent_by": {
"id": "FyXaS4SlT2FY7uLPqKD9f2",
"email": "john#appleseed.com",
"first_name": "John",
"last_name": "Appleseed",
"avatar": "https://pd-live-media.s3.amazonaws.com/users/FyXaS4SlT2FY7uLPqKD9f2/avatar.jpg"
},
"metadata": {
"salesforce_opp_id": "123456",
"my_favorite_pet": "Panda"
},
"tokens": [
{
"name": "Favorite Animal",
"value": "Panda"
}
],
"fields": [
{
"uuid": "YcLBNUKcx45UFxAK3NjLIH",
"name": "Textfield",
"title": "Favorite Animal",
"value": "Panda",
"assigned_to": {
"id": "FyXaS4SlT2FY7uLPqKD9f2",
"email": "john#appleseed.com",
"first_name": "John",
"last_name": "Appleseed",
"role": "Signer",
"recipient_type": "signer",
"has_completed": true,
"type": "recipient"
}
}
],
"pricing": {
"tables": [
{
"id": 82307036,
"name": "PricingTable1",
"is_included_in_total": true,
"summary": {
"discount": 10,
"tax": 0,
"total": 60,
"subtotal": 60
},
"items": [
{
"id": "4ElJ4FEsG4PHAVNPR5qoo9",
"qty": 1,
"name": "Toy Panda",
"cost": "25",
"price": "53",
"description": "Buy a Panda",
"custom_fields": {
"sampleField": "Sample Field"
},
"custom_columns": {
"sampleColumn": "Sample Column"
},
"discount": 10,
"subtotal": 60
}
],
"total": 60
}
]
},
"tags": [
"test tag",
"sales",
"support"
]
}
}
]
I would really appreciate a tip. Thank you
If you store your JSON object into variable called obj you can access that value ("Toy Panda") with:
obj.data.pricing.tables[0].items[0].name
because tables and items are arrays.
Here is a sample example :
<script>
var data = '{"name": "mkyong","age": 30,"address": {"streetAddress": "88 8nd Street","city": "New York"},"phoneNumber": [{"type": "home","number": "111 111-1111"},{"type": "fax","number": "222 222-2222"}]}';
var json = JSON.parse(data);
alert(json["name"]); //mkyong
alert(json.name); //mkyong
</script>
Refer this link :[https://www.mkyong.com/javascript/how-to-access-json-object-in-javascript/][1]
In your case it should be something like :
var data = // your json;
var json = JSON.parse(data);
console.log(json.pricing.tables.items[0].name;

Create Parent and Child from JSON

I need to implement one collapsible diagram using d3. But first, it is necessary to parse my json file in order to create correct hierachies.
This is my json file example which I need transform:
{"likes":[{"_id":null,"category":"Music","link":"https://www.facebook.com/Brum","name":"Brum"},
{"_id":null,"category":"Music","link":"https://www.facebook.com/pan", "name":"Pan FM"},
{"_id":null,"category":"Music","link":"https://www.facebook.com/Example","name":"Example"},
{"_id":null,"category":"Books","link":"https://www.facebook.com/foo","name":"Foo"},
{"_id":null,"category":"Movies","link":"https://www.facebook.com/Titanic","name":"Titanic"}]}]}
And this the structure that I need extract from my json file:
var Data = [{
"name": "SocialProfiles",
"parent": "null",
"children": [{
"name": "Likes",
"parent": "SocialProfiles",
"children": [{
"name": "Music",
"parent": "Likes",
"children": [{
"parent": "Music",
"name": "Brum"
}, {
"parent": "Music",
"name": "Pan FM"
}, {
"parent": "Music",
"name": "Example"
}]
},
{
"name": "Books",
"parent": "Likes",
"children": [{
"parent": "Books",
"name": "Foo"
}, {
"parent": "Books",
"name": "Foo"
}]
}, {
"name": "Movies",
"parent": "Likes",
"children": [{
"parent": "Movies",
"name": "Titanic"
}]
}
]
}]
}];

Replace the JSON object after updating using javascript

This is a follow up quesstion for JSON replacement
where I was not able to get a proper response. Hence I'm posting this question with a better example.
var beforeReplacement=[
{
"Name": "app1",
"id": "1",
"groups": [
{
"id": "test1",
"name": "test grp45",
"desc": "this is a test group"
},
{
"id": "test2",
"name": "test group 2",
"desc": "this is another test group"
}
]
},
{
"Name": "app2",
"id": "2",
"groups": [
{
"id": "test3",
"name": "test group 4",
"desc": "this is a test group"
},
{
"id": "test4",
"name": "test group 4",
"desc": "this is another test group"
}
]
}
]
changed object:
[
{
"Name": "app2",
"id": "2",
"groups": [
{
"id": "test3",
"name": "changed test group 4",
"desc": "this is a test group"
}
]
}
]
var afterReplacement=[
{
"Name": "app1",
"id": "1",
"groups": [
{
"id": "test1",
"name": "test grp45",
"desc": "this is a test group"
},
{
"id": "test2",
"name": "test group 2",
"desc": "this is another test group"
}
]
},
{
"Name": "app2",
"id": "2",
"groups": [
{
"id": "test3",
"name": "changed test group 4",
"desc": "this is a test group"
},
{
"id": "test4",
"name": "test group 4",
"desc": "this is another test group"
}
]
}
]
I have changed the name in var beforeReplacement and have mentioned the modified object that I will be receiving after the changes. How can I efficiently replace this changed object in the beforeReplacement so that the resultant object will be like var afterReplacement
var afterReplacement = beforeReplacement.map(function (af) {
for(var i in changed) {
if (changed[i].id != af.id) continue;
af = changed[i];
break;
}
return af;
});

Categories

Resources