Vuejs How to assign array object? - javascript

I have a response like below from an API call,
{
"1-2021": [
{
"id": 1,
"status": "New",
"player_count": 7
},
{
"id": 2,
"status": "Verified",
"player_count": 4
},
{
"id": 3,
"status": "Regitered ID",
"player_count": 18
},
{
"id": 4,
"status": "On Playing",
"player_count": 15
},
{
"id": 5,
"status": "Finished",
"player_count": 9
},
{
"id": 6,
"status": "Active",
"player_count": 10
},
{
"id": 7,
"status": "Inactive",
"player_count": 0
}
],
"2-2021": [
{
"id": 1,
"status": "New",
"player_count": 3
},
{
"id": 2,
"status": "Verified",
"player_count": 8
},
{
"id": 3,
"status": "Regitered ID",
"player_count": 17
},
{
"id": 4,
"status": "On Playing",
"player_count": 11
},
{
"id": 5,
"status": "Finished",
"player_count": 7
},
{
"id": 6,
"status": "Active",
"player_count": 6
},
{
"id": 7,
"status": "Inactive",
"player_count": 0
}
]
}
Then, I have to repeat the whole arrays inside the arrays. How do I do that in VueJS?
I have searched for using forEach.. nowhere I found forEach usage.
Can anyone help me on how to fetch the values from that arrays by using either forEach or any else(VueJS)?
I expected the outcome:
chartData: [
['Month', 'New', 'Verified', 'Regitered ID', 'On Playing', 'Finished', 'Active', 'Inactive'],
['January', 7, 4, 18, 15, 9, 10, 0],
['February', 16, 22, 23, 30, 16, 9, 8]
]
Thanks & Regards,

Try this
let a = {
"1-2021": [{
"id": 1,
"status": "New",
"player_count": 7
},
{
"id": 2,
"status": "Verified",
"player_count": 4
},
{
"id": 3,
"status": "Regitered ID",
"player_count": 18
},
{
"id": 4,
"status": "On Playing",
"player_count": 15
},
{
"id": 5,
"status": "Finished",
"player_count": 9
},
{
"id": 6,
"status": "Active",
"player_count": 10
},
{
"id": 7,
"status": "Inactive",
"player_count": 0
}
],
"2-2021": [{
"id": 1,
"status": "New",
"player_count": 3
},
{
"id": 2,
"status": "Verified",
"player_count": 8
},
{
"id": 3,
"status": "Regitered ID",
"player_count": 17
},
{
"id": 4,
"status": "On Playing",
"player_count": 11
},
{
"id": 5,
"status": "Finished",
"player_count": 7
},
{
"id": 6,
"status": "Active",
"player_count": 6
},
{
"id": 7,
"status": "Inactive",
"player_count": 0
}
]
};
let ar = [];
let b = Object.keys(a).forEach((e, index) => {
if (index == 0) {
let b = a[e].map(r => r.status)
b.unshift("Month")
ar.push(b)
}
let a1 = [e]
a[e].forEach(c => {
a1.push(c.player_count)
c.status
})
ar.push(a1)
})
console.log(ar)

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
)

Javascript: Map array with objects from another array and return computed array

This might seem trivial but I'm having a hard time figuring this out.
I have an array like below:
Array 1:
[
{
"id": 1,
"date": "2019-03-27",
"time": 1,
"max_tasks": 3,
"reservations": [
5,
2
]
},
{
"id": 2,
"date": "2019-03-28",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 3,
"date": "2019-03-29",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 4,
"date": "2019-03-30",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 5,
"date": "2019-03-31",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 6,
"date": "2019-04-01",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 7,
"date": "2019-04-02",
"time": 1,
"max_tasks": 3,
"reservations": [
3
]
}
]
Here the reservations contains id for which i need to use another array as shown below:
Array 2:
[
{
"id": 5,
"app": 1,
"comment": "test 5"
},
{
"id": 2,
"app": 1,
"comment": "test 2"
}
]
Expected:
I need a way to include Array 2 data inside Array 1 where I use the id from reservations of Array 1 and pull the corresponding object from Array 2. Finally I need to return Array 1 with these objects added from Array 2 .
I'm using Vue and hope to get this array computed in getters so I can get this in my component and map there directly. If there are any better ways of doing this I'm open to any suggestions
This is what I'm trying to get as output:
`
[
{
'id': 1,
'date': '2019-03-27',
'time': 1,
'max_tasks': 3,
'reservations': [
{
'id': 5,
'app': 1,
'comment': 'test 5'
},
{
'id': 2,
'app': 1,
'comment': 'test 2'
}
]
},
{
'id': 2,
'date': '2019-03-28',
'time': 1,
'max_tasks': 3,
'reservations': []
},
{
'id': 3,
'date': '2019-03-29',
'time': 1,
'max_tasks': 3,
'reservations': []
},
{
'id': 4,
'date': '2019-03-30',
'time': 1,
'max_tasks': 3,
'reservations': []
},
{
'id': 5,
'date': '2019-03-31',
'time': 1,
'max_tasks': 3,
'reservations': []
},
{
'id': 6,
'date': '2019-04-01',
'time': 1,
'max_tasks': 3,
'reservations': []
},
{
'id': 7,
'date': '2019-04-02',
'time': 1,
'max_tasks': 3,
'reservations': [
3
]
}
]
`
Thanks for your time.
It may not be the best solution but give it a try.
let arr1 = [{
"id": 1,
"date": "2019-03-27",
"time": 1,
"max_tasks": 3,
"reservations": [
5,
2
]
},
{
"id": 2,
"date": "2019-03-28",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 3,
"date": "2019-03-29",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 4,
"date": "2019-03-30",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 5,
"date": "2019-03-31",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 6,
"date": "2019-04-01",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 7,
"date": "2019-04-02",
"time": 1,
"max_tasks": 3,
"reservations": [
3
]
}
]
let arr2 = [{
"id": 5,
"app": 1,
"comment": "test 5"
},
{
"id": 2,
"app": 1,
"comment": "test 2"
}
]
arr1.forEach(o => {
let updatedReverations = []
o.reservations.forEach(r => {
updatedReverations.push(arr2.filter(a2 => r === a2.id)[0] || r)
})
o.reservations = updatedReverations
})
console.log(arr1)
perhaps this works
var arr1 = [
{
"id": 1,
"date": "2019-03-27",
"time": 1,
"max_tasks": 3,
"reservations": [
5,
2
]
},
{
"id": 2,
"date": "2019-03-28",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 3,
"date": "2019-03-29",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 4,
"date": "2019-03-30",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 5,
"date": "2019-03-31",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 6,
"date": "2019-04-01",
"time": 1,
"max_tasks": 3,
"reservations": []
},
{
"id": 7,
"date": "2019-04-02",
"time": 1,
"max_tasks": 3,
"reservations": [
3
]
}
]
var arr2 = [
{
"id": 5,
"app": 1,
"comment": "test 5"
},
{
"id": 2,
"app": 1,
"comment": "test 2"
}
]
for (var i = 0; i != arr1.length; ++i) {
arr1[i].reservations = arr1[i].reservations.map(id => {
var reservation = arr2.find(reservation => reservation.id == id)
return reservation || id;
});
}
It will try to find reservation in array 2 if not it will not replace the id.
below function will give you new copy of merged array.
function getMergedArray(arr1,arr2){
return arr1.map(item=>{
if(item.reservations.length>0){
let newReservations= item.reservations.map(reservationId=>{
let foundReservation = arr2.find(reservation=>{
return reservation.id === reservationId
})
if(foundReservation){
return foundReservation;
}
else{
return reservationId;
}
})
item.reservations = newReservations;
}
return item;
});
}

How do I flatten a (forest of) trees?

I have a forest of trees of arbitrary height, more or less like this:
let data = [
{ "id": 2, "name": "AAA", "parent_id": null, "short_name": "A" },
{
"id": 10, "name": "BBB", "parent_id": null, "short_name": "B", "children": [
{
"id": 3, "name": "CCC", "parent_id": 10, "short_name": "C", "children": [
{ "id": 6, "name": "DDD", "parent_id": 3, "short_name": "D" },
{ "id": 5, "name": "EEE", "parent_id": 3, "short_name": "E" }
]
},
{
"id": 4, "name": "FFF", "parent_id": 10, "short_name": "F", "children": [
{ "id": 7, "name": "GGG", "parent_id": 4, "short_name": "G" },
{ "id": 8, "name": "HHH", "parent_id": 4, "short_name": "H" }
]
}]
}
];
And I'm trying to produce a representation of all the root-to-leaves paths, something like this
[
[
{
"id": 2,
"name": "AAA"
}
],
[
{
"id": 10,
"name": "B"
},
{
"id": 3,
"name": "C"
},
{
"id": 6,
"name": "DDD"
}
],
[
{
"id": 10,
"name": "B"
},
{
"id": 3,
"name": "C"
},
{
"id": 5,
"name": "EEE"
}
],
[
{
"id": 10,
"name": "B"
},
{
"id": 4,
"name": "F"
},
{
"id": 7,
"name": "GGG"
}
],
[
{
"id": 10,
"name": "B"
},
{
"id": 4,
"name": "F"
},
{
"id": 8,
"name": "HHH"
}
]
]
So I wrote the following code:
function flattenTree(node, path = []) {
if (node.children) {
return node.children.map(child => flattenTree(child, [...path, child]));
} else {
let prefix = path.slice(0, path.length - 1).map(n => ({ id: n.id, name: n.short_name }));
let last = path[path.length - 1];
return [...prefix, { id: last.id, name: last.name } ];
}
}
let paths = data.map(n => flattenTree(n, [n]));
but paths comes out with extra nesting, like this:
[
[
{
"id": 2,
"name": "AAA"
}
],
[
[
[
{
"id": 10,
"name": "B"
},
{
"id": 3,
"name": "C"
},
{
"id": 6,
"name": "DDD"
}
],
[
{
"id": 10,
"name": "B"
},
{
"id": 3,
"name": "C"
},
{
"id": 5,
"name": "EEE"
}
]
],
[
[
{
"id": 10,
"name": "B"
},
{
"id": 4,
"name": "F"
},
{
"id": 7,
"name": "GGG"
}
],
[
{
"id": 10,
"name": "B"
},
{
"id": 4,
"name": "F"
},
{
"id": 8,
"name": "HHH"
}
]
]
]
]
I lost count of the many ways in which I tried to fix this, but it does look like the algorithm should not produce the extra nesting -- or my eyes are just so crossed by now that I couldn't see my mistake if someone stuck their finger on it.
Can someone help? Feel free to peruse this JSFiddle https://jsfiddle.net/png7x9bh/66/
The extra nestings are created by map. map just wraps the results into an array and returns them, it doesn't care if it is called on child nodes or not. Use reduce and just concat (or push, whatever suits your performance) the results into the first level array directly:
let data = [{"id":2,"name":"AAA","parent_id":null,"short_name":"A"},{"id":10,"name":"BBB","parent_id":null,"short_name":"B","children":[{"id":3,"name":"CCC","parent_id":10,"short_name":"C","children":[{"id":6,"name":"DDD","parent_id":3,"short_name":"D"},{"id":5,"name":"EEE","parent_id":3,"short_name":"E"}]},{"id":4,"name":"FFF","parent_id":10,"short_name":"F","children":[{"id":7,"name":"GGG","parent_id":4,"short_name":"G"},{"id":8,"name":"HHH","parent_id":4,"short_name":"H"}]}]}];
function flattenTree(node, path = []) {
let pathCopy = Array.from(path);
pathCopy.push({id: node.id, name: node.name});
if(node.children) {
return node.children.reduce((acc, child) => acc.concat(flattenTree(child, pathCopy)), []);
}
return [pathCopy];
}
let result = data.reduce((result, node) => result.concat(flattenTree(node)), []);
console.log(JSON.stringify(result, null, 3));

How to find all unique paths in tree structure

root1
child1
child2
grandchild1
grandchild2
child3
root2
child1
child2
grandchild1
greatgrandchild1
I have an object array like tree structure like above, I want to get all unique paths in like this
Food->Dry Food Items->Local Dry Food Items
Food->Dry Food Items->Thai Dry Food Items
Food->Dry Food Items->Others
Food->Fruits
------
------
This is my object
[
{
"id": 1,
"name": "Food",
"parent_id": 0,
"children": [
{
"id": 5,
"name": "Dry Food Items",
"parent_id": 1,
"children": [
{
"id": 11,
"name": "Local Dry Food Items",
"parent_id": 5
},
{
"id": 12,
"name": "Thai Dry Food Items",
"parent_id": 5
},
{
"id": 60,
"name": "Others",
"parent_id": 5
}
]
},
{
"id": 6,
"name": "Fruits",
"parent_id": 1
},
{
"id": 7,
"name": "LG Branded",
"parent_id": 1
},
{
"id": 8,
"name": "Meat",
"parent_id": 1
},
{
"id": 9,
"name": "Sea food",
"parent_id": 1
},
{
"id": 10,
"name": "Vegetables",
"parent_id": 1,
"children": [
{
"id": 14,
"name": "Local Vegetables",
"parent_id": 10
},
{
"id": 15,
"name": "Thai Vegetables",
"parent_id": 10
}
]
},
{
"id": 38,
"name": "Frozen",
"parent_id": 1
},
{
"id": 39,
"name": "IP Kitchen",
"parent_id": 1,
"children": [
{
"id": 40,
"name": "IP Meat",
"parent_id": 39
},
{
"id": 41,
"name": "IP Starter",
"parent_id": 39
},
{
"id": 42,
"name": "IP Ingredients",
"parent_id": 39
},
{
"id": 43,
"name": "IP Sauce",
"parent_id": 39
},
{
"id": 44,
"name": "IP Seafood",
"parent_id": 39
},
{
"id": 45,
"name": "IP Starter",
"parent_id": 39
},
{
"id": 46,
"name": "IP Desert",
"parent_id": 39
}
]
}
]
},
{
"id": 2,
"name": "Beverage",
"parent_id": 0,
"children": [
{
"id": 16,
"name": "Bar",
"parent_id": 2
},
{
"id": 17,
"name": "Coffee & Tea",
"parent_id": 2
},
{
"id": 18,
"name": "In Can",
"parent_id": 2
},
{
"id": 19,
"name": "Water",
"parent_id": 2
},
{
"id": 47,
"name": "IP Bar",
"parent_id": 2
}
]
},
{
"id": 3,
"name": "Disposable",
"parent_id": 0,
"children": [
{
"id": 21,
"name": "Disposable",
"parent_id": 3
}
]
},
{
"id": 4,
"name": "SOE",
"parent_id": 0,
"children": [
{
"id": 20,
"name": "Cleaning Materials",
"parent_id": 4
},
{
"id": 22,
"name": "Chinaware",
"parent_id": 4
}
]
}
];
I get to all the nodes in the tree
function traverse(categories) {
categories.forEach(function (category) {
if (category.children && category.children.length) {
traverse(category.children);
}
else {
}
}, this);
}
You can use recursion and create a function using forEach loop.
var arr = [{"id":1,"name":"Food","parent_id":0,"children":[{"id":5,"name":"Dry Food Items","parent_id":1,"children":[{"id":11,"name":"Local Dry Food Items","parent_id":5},{"id":12,"name":"Thai Dry Food Items","parent_id":5},{"id":60,"name":"Others","parent_id":5}]},{"id":6,"name":"Fruits","parent_id":1},{"id":7,"name":"LG Branded","parent_id":1},{"id":8,"name":"Meat","parent_id":1},{"id":9,"name":"Sea food","parent_id":1},{"id":10,"name":"Vegetables","parent_id":1,"children":[{"id":14,"name":"Local Vegetables","parent_id":10},{"id":15,"name":"Thai Vegetables","parent_id":10}]},{"id":38,"name":"Frozen","parent_id":1},{"id":39,"name":"IP Kitchen","parent_id":1,"children":[{"id":40,"name":"IP Meat","parent_id":39},{"id":41,"name":"IP Starter","parent_id":39},{"id":42,"name":"IP Ingredients","parent_id":39},{"id":43,"name":"IP Sauce","parent_id":39},{"id":44,"name":"IP Seafood","parent_id":39},{"id":45,"name":"IP Starter","parent_id":39},{"id":46,"name":"IP Desert","parent_id":39}]}]},{"id":2,"name":"Beverage","parent_id":0,"children":[{"id":16,"name":"Bar","parent_id":2},{"id":17,"name":"Coffee & Tea","parent_id":2},{"id":18,"name":"In Can","parent_id":2},{"id":19,"name":"Water","parent_id":2},{"id":47,"name":"IP Bar","parent_id":2}]},{"id":3,"name":"Disposable","parent_id":0,"children":[{"id":21,"name":"Disposable","parent_id":3}]},{"id":4,"name":"SOE","parent_id":0,"children":[{"id":20,"name":"Cleaning Materials","parent_id":4},{"id":22,"name":"Chinaware","parent_id":4}]}]
function getNames(data) {
var result = [];
function loop(data, c) {
data.forEach(function (e) {
var name = !c.length ? e.name : c + '->' + e.name;
if (e.children) { loop(e.children, name); }
else {
result.push({ name: name });
}
});
}
loop(data, '');
return result;
}
console.log(getNames(arr))

How to collect value in javascript?

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);

Categories

Resources