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);
Related
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
)
I'm trying to filter through an array of objects it's children then update the children and return the parent with updated children.
Example array of objects:
[
{
"id": 1,
"name": "group1",
"users": [
{
"id": 1,
"name": "Mike"
},
{
"id": 2,
"name": "Steve"
},
{
"id": 3,
"name": "John"
}
]
},
{
"id": 2,
"name": "group2",
"users": [
{
"id": 4,
"name": "Phill"
},
{
"id": 5,
"name": "Joe"
},
{
"id": 6,
"name": "Dominik"
}
]
}
]
I've got an input in React where someone can type in a name of an user, then i'd like to only show the users with that name while keeping the group that they're in.
For example, if the input is 'Mike' I would like the result to be:
[
{
"id": 1,
"name": "group1",
"users": [
{
"id": 1,
"name": "Mike"
}
]
},
{
"id": 2,
"name": "group2",
"users": []
}
]
You can use map() and filter() to do that.
let arr = [ { "id": 1, "name": "group1", "users": [ { "id": 1, "name": "Mike" }, { "id": 2, "name": "Steve" }, { "id": 3, "name": "John" } ] }, { "id": 2, "name": "group2", "users": [ { "id": 4, "name": "Phill" }, { "id": 5, "name": "Joe" }, { "id": 6, "name": "Dominik" } ] } ];
function filterUsers(arr, name) {
return arr.map(obj => {
return {
...obj,
"users": obj.users.filter(user => user.name === name)
};
});
}
console.log(filterUsers(arr, "Mike"));
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);
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);
I have an array like this in Javascript. Something like this
[
{
"id": 1,
"facilities": [
{
"id": 10,
"name": "Wifi",
"label": "Wifi"
},
{
"id": 12,
"name": "Toll",
"label": "Toll"
}
]
},
{
"id": 2,
"facilities": [
{
"id": 10,
"name": "Wifi",
"label": "Wifi"
},
{
"id": 12,
"name": "Toll",
"label": "Toll"
},
{
"id": 13,
"name": "Snack",
"label": "Snack"
}
]
},
{
"id": 3,
"facilities": [
{
"id": 10,
"name": "Wifi",
"label": "Wifi"
},
{
"id": 12,
"name": "Toll",
"label": "Toll"
},
{
"id": 14,
"name": "Petrol",
"label": "Petrol"
}
]
}
]
I want to collect data and grouping data facilities of the array in Javascript, something like this.
"facilities": [
{
"id": 10,
"name": "Wifi",
"label": "Wifi"
},
{
"id": 12,
"name": "Toll",
"label": "Toll"
},
{
"id": 13,
"name": "Snack",
"label": "Snack"
},
{
"id": 14,
"name": "Petrol",
"label": "Petrol"
}
]
So, basically, group by facilities. I just don't know how to handle the grouping of similar facilities values.
Assuming the facility ids are unique:
const facilities = input.reduce((memo, entry) => {
entry.facilities.forEach((f) => {
if (!memo.some((m) => m.id === f.id)) {
memo.push(f)
}
})
return memo
}, [])
You can iterate through all rows and collect (id, entity) map.
Index map allows us to not search already collected entities every time.
Then you can convert it to an array with object keys mapping.
let input = [
{"id": 1, "facilities": [{"id": 10, "name": "Wifi", "label": "Wifi"}, {"id": 12, "name": "Toll", "label": "Toll"} ] },
{"id": 2, "facilities": [{"id": 10, "name": "Wifi", "label": "Wifi"}, {"id": 12, "name": "Toll", "label": "Toll"}, {"id": 13, "name": "Snack", "label": "Snack"} ] },
{"id": 3, "facilities": [{"id": 10, "name": "Wifi", "label": "Wifi"}, {"id": 12, "name": "Toll", "label": "Toll"}, {"id": 14, "name": "Petrol", "label": "Petrol"} ] }
];
let index = input.reduce((res, row) => {
row.facilities.forEach(f => res[f.id] = f);
return res;
}, {});
let result = Object.keys(index).map(id => index[id]);
console.log({facilities: result});
You could use a Set for flagging inserted objects with the given id.
var data = [{ id: 1, facilities: [{ id: 10, name: "Wifi", label: "Wifi" }, { id: 12, name: "Toll", label: "Toll" }] }, { id: 2, facilities: [{ id: 10, name: "Wifi", label: "Wifi" }, { id: 12, name: "Toll", label: "Toll" }, { id: 13, name: "Snack", label: "Snack" }] }, { id: 3, facilities: [{ id: 10, name: "Wifi", label: "Wifi" }, { id: 12, name: "Toll", label: "Toll" }, { id: 14, name: "Petrol", label: "Petrol" }] }],
grouped = data.reduce(
(s => (r, a) => (a.facilities.forEach(b => !s.has(b.id) && s.add(b.id) && r.push(b)), r))(new Set),
[]
);
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
The solution using Array.prototype.reduce() and Set object:
var data = [{"id": 1,"facilities": [{"id": 10,"name": "Wifi","label": "Wifi"},{"id": 12,"name": "Toll","label": "Toll"}]},{"id": 2,"facilities": [{"id": 10,"name": "Wifi","label": "Wifi"},{"id": 12,"name": "Toll","label": "Toll"},{"id": 13,"name": "Snack","label": "Snack"}]},{"id": 3,"facilities": [{"id": 10,"name": "Wifi","label": "Wifi"},{"id": 12,"name": "Toll","label": "Toll"},{"id": 14,"name": "Petrol","label": "Petrol"}]}
];
var ids = new Set(),
result = data.reduce(function (r, o) {
o.facilities.forEach(function(v) { // iterating through nested `facilities`
if (!ids.has(v.id)) r.facilities.push(v);
ids.add(v.id); // saving only items with unique `id`
});
return r;
}, {facilities: []});
console.log(result);
const input = [
{
"id": 1,
"facilities": [
{
"id": 10,
"name": "Wifi",
"label": "Wifi"
},
{
"id": 12,
"name": "Toll",
"label": "Toll"
}
]
},
{
"id": 2,
"facilities": [
{
"id": 10,
"name": "Wifi",
"label": "Wifi"
},
{
"id": 12,
"name": "Toll",
"label": "Toll"
},
{
"id": 13,
"name": "Snack",
"label": "Snack"
}
]
},
{
"id": 3,
"facilities": [
{
"id": 10,
"name": "Wifi",
"label": "Wifi"
},
{
"id": 12,
"name": "Toll",
"label": "Toll"
},
{
"id": 14,
"name": "Petrol",
"label": "Petrol"
}
]
}
]
const result = []
const idx = []
for (const item of input) {
for (const facilityItem of item.facilities) {
if (!idx.includes(facilityItem.id)) {
idx.push(facilityItem.id)
result.push(facilityItem)
}
}
}
console.log(result)
A very simple and easily understood approach.
const data = [{
"id": 1,
"facilities": [{
"id": 10,
"name": "Wifi",
"label": "Wifi"
},
{
"id": 12,
"name": "Toll",
"label": "Toll"
}
]
},
{
"id": 2,
"facilities": [{
"id": 10,
"name": "Wifi",
"label": "Wifi"
},
{
"id": 12,
"name": "Toll",
"label": "Toll"
},
{
"id": 13,
"name": "Snack",
"label": "Snack"
}
]
},
{
"id": 2,
"facilities": [{
"id": 10,
"name": "Wifi",
"label": "Wifi"
},
{
"id": 12,
"name": "Toll",
"label": "Toll"
},
{
"id": 14,
"name": "Petrol",
"label": "Petrol"
}
]
}
];
let o = {};
let result = [];
data.forEach((d) => {
d.facilities.forEach((f) => {
o[f.id] = f;
});
});
for (let r in o) {
result.push(o[r]);
}
console.log(result);