How can I filter specific object value by the parameter of another object value - javascript

This is the input. In the "posts" property, I have all of the posts. I need to filter the posts by "postIds" property.
const users = [
{ id: 1, name: 'Dan', postIds: [11, 22],posts:[
{ id: 11, title: 'How to make it fast' },
{ id: 22, title: 'How to make it clearly' },
{ id: 33, title: 'How to can I do it' },
{ id: 44, title: 'How to he can do it' },
]} ,
{ id: 2, name: 'Mike', postIds: [33], posts:[
{ id: 11, title: 'How to make it fast' },
{ id: 22, title: 'How to make it clearly' },
{ id: 33, title: 'How to can I do it' },
{ id: 44, title: 'How to he can do it' },
]},
{ id: 3, name: 'Lola', postIds: [44], posts: [
{ id: 11, title: 'How to make it fast' },
{ id: 22, title: 'How to make it clearly' },
{ id: 33, title: 'How to can I do it' },
{ id: 44, title: 'How to he can do it' },
]}
This is how the output should look alike:
const users = [
{ id: 1, name: 'Dan', postIds: [11, 22],posts:[
{ id: 11, title: 'How to make it fast' },
{ id: 22, title: 'How to make it clearly' },
] ,
{ id: 2, name: 'Mike', postIds: [33], posts:[
{ id: 33, title: 'How to can I do it' },
]},
{ id: 3, name: 'Lola', postIds: [44], posts: [
{ id: 44, title: 'How to he can do it' },
]}
I do believe that I have to work with "filter" and "some", but the option that I tried is not relevantly working:
const filterUserPosts = users.filter(obj=>obj.posts.id===obj.some(postIds))
What is the best option?

The array you want to filter is obj.posts, not updUsers (which I assume is the same as users).
You need to loop over users to filter each of their posts properties.
You can use includes() to tell if the id of the post is in postIds.
const users = [
{ id: 1, name: 'Dan', postIds: [11, 22],posts:[
{ id: 11, title: 'How to make it fast' },
{ id: 22, title: 'How to make it clearly' },
{ id: 33, title: 'How to can I do it' },
{ id: 44, title: 'How to he can do it' },
]} ,
{ id: 2, name: 'Mike', postIds: [33], posts:[
{ id: 11, title: 'How to make it fast' },
{ id: 22, title: 'How to make it clearly' },
{ id: 33, title: 'How to can I do it' },
{ id: 44, title: 'How to he can do it' },
]},
{ id: 3, name: 'Lola', postIds: [44], posts: [
{ id: 11, title: 'How to make it fast' },
{ id: 22, title: 'How to make it clearly' },
{ id: 33, title: 'How to can I do it' },
{ id: 44, title: 'How to he can do it' },
]}];
users.forEach(u => u.posts = u.posts.filter(p => u.postIds.includes(p.id)));
console.log(users);

You need to save the postIds array somewhere first, then call .filter on the posts array, filtering by whether the element being iterated over has an ID in the postIds:
const users = [
{ id: 1, name: 'Dan', postIds: [11, 22],posts:[
{ id: 11, title: 'How to make it fast' },
{ id: 22, title: 'How to make it clearly' },
{ id: 33, title: 'How to can I do it' },
{ id: 44, title: 'How to he can do it' },
]} ,
{ id: 2, name: 'Mike', postIds: [33], posts:[
{ id: 11, title: 'How to make it fast' },
{ id: 22, title: 'How to make it clearly' },
{ id: 33, title: 'How to can I do it' },
{ id: 44, title: 'How to he can do it' },
]},
{ id: 3, name: 'Lola', postIds: [44], posts: [
{ id: 11, title: 'How to make it fast' },
{ id: 22, title: 'How to make it clearly' },
{ id: 33, title: 'How to can I do it' },
{ id: 44, title: 'How to he can do it' },]}
]
const output = users.map(
user => ({
...user,
posts: user.posts.filter(
post => user.postIds.includes(post.id)
)
})
);
console.log(output);

Try this:
const usersWithFilteredPosts = users.map((user) => ({
...user,
posts: user.posts.filter(({ id }) => user.postIds.includes(id)),
}));

Related

Lodash groupBy id of each item of nested array of object

I have the following input as an example of clubs that includes a property players which is an array of objects.
Input
const clubs = [
{
id: 5,
name: 'Club Name',
creatorId: 10,
players: [
{
userId: 2, // group by this property
name: 'Player name 1',
clubId: 5,
},
{
userId: 7, // group by this property
name: 'Player name 2',
clubId: 5,
},
],
},
{
id: 6,
name: 'Club Name 2',
creatorId: 2,
players: [
{
userId: 7, // group by this property
name: 'Player name 3',
clubId: 6,
},
{
userId: 8, // group by this property
name: 'Player name 4',
clubId: 6,
},
{
userId: 22, // group by this property
name: 'Player name 5',
clubId: 6,
},
],
},
];
I want to groupBy each of the player.userIds of each club and should have a value of the club for each player, to have the following output.
Desired Output
{
'2': [{ id: 5, name: 'Club Name', creatorId: 10, players: [Array] }],
'7': [
{ id: 5, name: 'Club Name', creatorId: 10, players: [Array] },
{ id: 6, name: 'Club Name 2', creatorId: 2, players: [Array] },
],
'8': [{ id: 6, name: 'Club Name 2', creatorId: 2, players: [Array] }],
'22': [{ id: 6, name: 'Club Name 2', creatorId: 2, players: [Array] }],
};
I have tried
const byPlayer = allClubs.reduce((b, a) => {
a.players.forEach((player) => {
const id = player.clubId;
const clubsByPlayer = b[id] || (b[id] = []);
clubsByPlayer.push(a);
});
return b;
}, {});
But it returned the group by the clubId and a value of each player in the club
{
'5': [
{ id: 5, name: 'Club Name', creatorId: 10, players: [Array] },
{ id: 5, name: 'Club Name', creatorId: 10, players: [Array] },
],
'6': [
{ id: 6, name: 'Club Name 2', creatorId: 2, players: [Array] },
{ id: 6, name: 'Club Name 2', creatorId: 2, players: [Array] },
{ id: 6, name: 'Club Name 2', creatorId: 2, players: [Array] },
],
};
Replace
const id = player.clubId;
with
const id = player.userId;
const
clubs = [{ id: 5, name: 'Club Name', creatorId: 10, players: [{ userId: 2, name: 'Player name 1', clubId: 5 }, { userId: 7, name: 'Player name 2', clubId: 5 }] }, { id: 6, name: 'Club Name 2', creatorId: 2, players: [{ userId: 7, name: 'Player name 3', clubId: 6 }, { userId: 8, name: 'Player name 4', clubId: 6 }, { userId: 22, name: 'Player name 5', clubId: 6 }] }],
byPlayer = clubs.reduce((b, a) => {
a.players.forEach((player) => {
(b[player.userId] ??= []).push(a);
});
return b;
}, {});
console.log(byPlayer)
.as-console-wrapper { max-height: 100% !important; top: 0; }

find method not returning '0th' matched object react, or react-native or javascript

Not getting all matched objects, its is returning all objects except '{ ID:0, Name: 'General Group' }', i want all matched objects
let arrDashboardIconGroups = [
{ ID:0, Name: 'General Group' },
{ ID: 1, Name: 'Patient Administration' },
{ ID: 2, Name: 'Medical Charts' },
{ ID: 3, Name: 'Medical Procedures' },
{ ID: 13, Name: 'Purchase' },
{ ID: 14, Name: 'Sales' },
{ ID: 5, Name: 'Insurance' },
{ ID: 4, Name: 'Cash' },
{ ID: 6, Name: 'Pharmacy' },
{ ID: 7, Name: 'Inventory' },
{ ID: 8, Name: 'Lab' },
{ ID: 9, Name: 'Imaging' },
{ ID: 10, Name: 'In Patient' },
{ ID: 11, Name: 'System Administration' },
{ ID: 12, Name: 'Accounting' }
]
const getMatchedobjects=()=> {
let result = [0,1,2,3,4,]
let matchedArray = arrDashboardIconGroups.filter(val =>result.find(
(items)=>items == val.ID))
console.log(matchedArray)
}
getMatchedobjects()
You are returning the value of
result.find((items) => items == val.ID)
In the first case, the value returned is 0 which is a falsy value. So It won't include in the final filter result.
You can run the below code and see the returning values.
let arrDashboardIconGroups = [
{ ID: 0, Name: "General Group" },
{ ID: 1, Name: "Patient Administration" },
{ ID: 2, Name: "Medical Charts" },
{ ID: 3, Name: "Medical Procedures" },
{ ID: 13, Name: "Purchase" },
{ ID: 14, Name: "Sales" },
{ ID: 5, Name: "Insurance" },
{ ID: 4, Name: "Cash" },
{ ID: 6, Name: "Pharmacy" },
{ ID: 7, Name: "Inventory" },
{ ID: 8, Name: "Lab" },
{ ID: 9, Name: "Imaging" },
{ ID: 10, Name: "In Patient" },
{ ID: 11, Name: "System Administration" },
{ ID: 12, Name: "Accounting" },
];
const getMatchedobjects = () => {
let result = [0, 1, 2, 3, 4];
let matchedArray = arrDashboardIconGroups.filter((val) => {
const returnResult = result.find((items) => items == val.ID);
console.log(returnResult);
return returnResult;
// result.includes(val.ID)
});
console.log(matchedArray);
};
getMatchedobjects();
Alternatively, you can use includes
let arrDashboardIconGroups = [
{ ID: 0, Name: "General Group" },
{ ID: 1, Name: "Patient Administration" },
{ ID: 2, Name: "Medical Charts" },
{ ID: 3, Name: "Medical Procedures" },
{ ID: 13, Name: "Purchase" },
{ ID: 14, Name: "Sales" },
{ ID: 5, Name: "Insurance" },
{ ID: 4, Name: "Cash" },
{ ID: 6, Name: "Pharmacy" },
{ ID: 7, Name: "Inventory" },
{ ID: 8, Name: "Lab" },
{ ID: 9, Name: "Imaging" },
{ ID: 10, Name: "In Patient" },
{ ID: 11, Name: "System Administration" },
{ ID: 12, Name: "Accounting" },
];
const getMatchedobjects = () => {
let result = [0, 1, 2, 3, 4];
let matchedArray = arrDashboardIconGroups.filter((val) => result.includes(val.ID));
console.log(matchedArray);
};
getMatchedobjects();
Issue
Find returns a value/object (the first matching), but it seems you want to filter the arrDashboardIconGroups array by those that match an id specified in the result array. When result[0] is returned, 0 is the value, which is falsey, and the filter doesn't return the element from the arrDashboardIconGroups array.
Solution
Use Array.prototype.some to return the boolean that filter needs to include an element in the result array.
let arrDashboardIconGroups = [
{ ID:0, Name: 'General Group' },
{ ID: 1, Name: 'Patient Administration' },
{ ID: 2, Name: 'Medical Charts' },
{ ID: 3, Name: 'Medical Procedures' },
{ ID: 13, Name: 'Purchase' },
{ ID: 14, Name: 'Sales' },
{ ID: 5, Name: 'Insurance' },
{ ID: 4, Name: 'Cash' },
{ ID: 6, Name: 'Pharmacy' },
{ ID: 7, Name: 'Inventory' },
{ ID: 8, Name: 'Lab' },
{ ID: 9, Name: 'Imaging' },
{ ID: 10, Name: 'In Patient' },
{ ID: 11, Name: 'System Administration' },
{ ID: 12, Name: 'Accounting' }
];
const getMatchedobjects = () => {
let result = [0,1,2,3,4,];
let matchedArray = arrDashboardIconGroups.filter(val => result.some((items)=>items == val.ID));
console.log(matchedArray);
}
getMatchedobjects();
Alternatively you could also check that result array includes the matching id.
let arrDashboardIconGroups = [
{ ID:0, Name: 'General Group' },
{ ID: 1, Name: 'Patient Administration' },
{ ID: 2, Name: 'Medical Charts' },
{ ID: 3, Name: 'Medical Procedures' },
{ ID: 13, Name: 'Purchase' },
{ ID: 14, Name: 'Sales' },
{ ID: 5, Name: 'Insurance' },
{ ID: 4, Name: 'Cash' },
{ ID: 6, Name: 'Pharmacy' },
{ ID: 7, Name: 'Inventory' },
{ ID: 8, Name: 'Lab' },
{ ID: 9, Name: 'Imaging' },
{ ID: 10, Name: 'In Patient' },
{ ID: 11, Name: 'System Administration' },
{ ID: 12, Name: 'Accounting' }
];
const getMatchedobjects=()=> {
let result = [0,1,2,3,4,];
let matchedArray = arrDashboardIconGroups.filter(val => result.includes(val.ID)) ;
console.log(matchedArray);
}
getMatchedobjects();
find() returns value of the first element in the provided array that satisfies the provided testing function. Since 0 is a falsy value, it does not include it in the final array. See MDN Docs
You can map the result array, and use arrDashboardIconGroups's find function, and return the matching objects.
let arrDashboardIconGroups = [
{ ID: 0, Name: 'General Group' },
{ ID: 1, Name: 'Patient Administration' },
{ ID: 2, Name: 'Medical Charts' },
{ ID: 3, Name: 'Medical Procedures' },
{ ID: 13, Name: 'Purchase' },
{ ID: 14, Name: 'Sales' },
{ ID: 5, Name: 'Insurance' },
{ ID: 4, Name: 'Cash' },
{ ID: 6, Name: 'Pharmacy' },
{ ID: 7, Name: 'Inventory' },
{ ID: 8, Name: 'Lab' },
{ ID: 9, Name: 'Imaging' },
{ ID: 10, Name: 'In Patient' },
{ ID: 11, Name: 'System Administration' },
{ ID: 12, Name: 'Accounting' }
]
const getMatchedobjects=()=> {
let result = [0, 1,2,3,4,99] //99 for trial
let matchedArray = result.map((res)=>arrDashboardIconGroups.find((val)=>val.ID == res)).filter(Boolean);
console.log(matchedArray)
}
getMatchedobjects()

How to make the property of one array property of another (code optimization)

That's what I have
const users = [
{ id: 1, name: 'Mike', postIds: [11, 22] },
{ id: 2, name: 'Dan', postIds: [33] },
{ id: 3, name: 'Lance', postIds: [44] },
];
const posts = [
{ id: 11, title: 'How good is he' },
{ id: 22, title: 'How fast is he' },
{ id: 33, title: 'How to make it faster' },
{ id: 44, title: 'How can he do it' },
];
That's what I need to get in the output
const expectedResult = [
{
id: 1,
name: 'Mike',
posts: [
{ id: 11, title: 'How good is he' },
{ id: 22, title: 'How fast is he' },
]
},
{
id: 2,
name: 'Dan',
posts: [{ id: 33, title: 'How to make it faster' }]
},
{
id: 3,
name: 'Lance',
posts: [{ id: 44, title: 'How can he do it' }]
},
]
That's what I tried. It works, but it's stupid and I think it can be done in one operation. Please check what can I do make to it cleaner
const users = [
{ id: 1, name: 'Mike', postIds: [11, 22] },
{ id: 2, name: 'Dan', postIds: [33] },
{ id: 3, name: 'Lance', postIds: [44] },
];
const posts = [
{ id: 11, title: 'How good is he' },
{ id: 22, title: 'How fast is he' },
{ id: 33, title: 'How to make it faster' },
{ id: 44, title: 'How can he do it' },
];
let updUsers = users.map(obj => ({ ...obj,
posts: [...posts]
}))
const output = updUsers.map(
user => ({
...user,
posts: user.posts.filter(
post => user.postIds.includes(post.id)
)
})
);
const expectedOut = output.map(({
id,
name,
posts
}) => ({
id,
name,
posts
}))
console.log(expOut)
Turn the posts array into a mapping of post ID -> post object for faster lookup:
const postMap = new Map(posts.map((p) => [p.id, p]));
const expectedResult = users.map((u) => {
const newU = { ...u, posts: u.postIds.map((id) => postMap.get(id)) };
delete newU.postIds; // Remove the undesired `postIds` property from the copy
return newU;
});
console.log(expectedResult);
You can filter posts when you're mapping users rather than doing it as a second pass.
const users = [
{ id: 1, name: 'Mike', postIds: [11, 22] },
{ id: 2, name: 'Dan', postIds: [33] },
{ id: 3, name: 'Lance', postIds: [44] },
];
const posts = [
{ id: 11, title: 'How good is he' },
{ id: 22, title: 'How fast is he' },
{ id: 33, title: 'How to make it faster' },
{ id: 44, title: 'How can he do it' },
];
let expOut = users.map(({id, name, postIds}) => ({ id, name,
posts: posts.filter(({id}) => postIds.includes(id))
}))
console.log(expOut)
This is another naive method but time complexity is more.
P.S.: Time complexity is n^3
const users = [
{ id: 1, name: 'Mike', postIds: [11, 22] },
{ id: 2, name: 'Dan', postIds: [33] },
{ id: 3, name: 'Lance', postIds: [44] },
];
const posts = [
{ id: 11, title: 'How good is he' },
{ id: 22, title: 'How fast is he' },
{ id: 33, title: 'How to make it faster' },
{ id: 44, title: 'How can he do it' },
];
let updUsers = users.map(function(obj){
let postsArr = [];
for(i=0; i<obj.postIds.length; i++){
const postArr = posts.find((item) => item.id == obj.postIds[i]);
postsArr.push(postArr);
};
return{'id':obj.id,'name':obj.name,'posts':postsArr};});
console.log(updUsers);
You can first convert your posts array to key value pairs and take object using Object.fromEntries, now just map it. Here is an implementation:
const users = [ { id: 1, name: 'Mike', postIds: [11, 22] }, { id: 2, name: 'Dan', postIds: [33] }, { id: 3, name: 'Lance', postIds: [44] },];
const posts = [ { id: 11, title: 'How good is he' }, { id: 22, title: 'How fast is he' }, { id: 33, title: 'How to make it faster' }, { id: 44, title: 'How can he do it' },];
//convert posts to object and then map it:
const postObjects = Object.fromEntries(posts.map(p=>[p.id, p]));
const result = users.map(({id, name, postIds})=>({id, name, posts:postIds.map(p=>postObjects[p])}));
console.log(result);
We can simplify this using Array.prototype.reduce and Array.prototype..filter and use a Set for finding the right posts for a user for faster lookup:
const users = [
{ id: 1, name: 'Mike', postIds: [11, 22] },
{ id: 2, name: 'Dan', postIds: [33] },
{ id: 3, name: 'Lance', postIds: [44] },
];
const posts = [
{ id: 11, title: 'How good is he' },
{ id: 22, title: 'How fast is he' },
{ id: 33, title: 'How to make it faster' },
{ id: 44, title: 'How can he do it' },
];
const mapUsersByPost = (users, posts) => {
return users.reduce((acc, {id, name, postIds}) => {
const filteredPosts = posts.filter(({id}) => new Set(postIds).has(id));
acc.push({ id, name, posts: filteredPosts});
return acc;
}, []);
}
console.log(mapUsersByPost(users, posts));

How would I compare the two to properly sort in order?

Objective is to distinguish the two arrays, and console log the products array in order by its price range, I tried using a nested for-loop to see if this can work out but hasn't gone off in the right track, was also thinking about using lo dash as well. How would I be able to compare the id's between the two and push them in order by price?
/*
Your task is to write a function or set of functions that console logs out the products ordered by price
*/
var products = [
{ id: 1, name: 'Buzzy' },
{ id: 2, name: 'Bytrex' },
{ id: 3, name: 'CactiDance' },
{ id: 4, name: 'CactiLoops' },
{ id: 5, name: 'Cadaver Jelly' },
{ id: 6, name: 'Caffeine Serene' },
{ id: 7, name: 'Cajun Sation' },
{ id: 8, name: 'Call it Green' },
{ id: 9, name: 'Callflex' },
{ id: 10, name: 'Calling Card Shark' },
{ id: 11, name: 'Calque' },
{ id: 12, name: 'Camel Meal Tea' },
{ id: 13, name: 'Camelot Chamomile' },
{ id: 14, name: 'Campxotica' },
{ id: 15, name: 'Camus the Killer Tale' },
{ id: 16, name: 'Candecor' },
{ id: 17, name: 'Candelarium' },
{ id: 18, name: 'CandID' },
{ id: 19, name: 'Candlelight Vittles' },
{ id: 20, name: 'Candy Ask' },
{ id: 21, name: 'Candy Floss' },
];
var prices = [
{ id: 6, price: 55 },
{ id: 14, price: 22 },
{ id: 15, price: 57 },
{ id: 4, price: 41 },
{ id: 18, price: 9 },
{ id: 17, price: 3 },
{ id: 2, price: 73 },
{ id: 7, price: 43 },
{ id: 5, price: 78 },
{ id: 1, price: 91 },
{ id: 8, price: 58 },
{ id: 16, price: 69 },
{ id: 13, price: 74 },
{ id: 19, price: 14 },
{ id: 21, price: 25 },
{ id: 12, price: 84 },
{ id: 20, price: 8 },
{ id: 9, price: 94 },
{ id: 10, price: 36 },
{ id: 3, price: 34 },
{ id: 11, price: 71 },
];
You can use map & findIndex. map will return a new array. Inside the callback function of map use findIndex and use it to find the index of the object where the id matches.
var products = [{
id: 1,
name: 'Buzzy'
},
{
id: 2,
name: 'Bytrex'
},
{
id: 3,
name: 'CactiDance'
},
{
id: 4,
name: 'CactiLoops'
},
{
id: 5,
name: 'Cadaver Jelly'
},
{
id: 6,
name: 'Caffeine Serene'
},
{
id: 7,
name: 'Cajun Sation'
},
{
id: 8,
name: 'Call it Green'
},
{
id: 9,
name: 'Callflex'
},
{
id: 10,
name: 'Calling Card Shark'
},
{
id: 11,
name: 'Calque'
},
{
id: 12,
name: 'Camel Meal Tea'
},
{
id: 13,
name: 'Camelot Chamomile'
},
{
id: 14,
name: 'Campxotica'
},
{
id: 15,
name: 'Camus the Killer Tale'
},
{
id: 16,
name: 'Candecor'
},
{
id: 17,
name: 'Candelarium'
},
{
id: 18,
name: 'CandID'
},
{
id: 19,
name: 'Candlelight Vittles'
},
{
id: 20,
name: 'Candy Ask'
},
{
id: 21,
name: 'Candy Floss'
},
];
var prices = [{
id: 6,
price: 55
},
{
id: 14,
price: 22
},
{
id: 15,
price: 57
},
{
id: 4,
price: 41
},
{
id: 18,
price: 9
},
{
id: 17,
price: 3
},
{
id: 2,
price: 73
},
{
id: 7,
price: 43
},
{
id: 5,
price: 78
},
{
id: 1,
price: 91
},
{
id: 8,
price: 58
},
{
id: 16,
price: 69
},
{
id: 13,
price: 74
},
{
id: 19,
price: 14
},
{
id: 21,
price: 25
},
{
id: 12,
price: 84
},
{
id: 20,
price: 8
},
{
id: 9,
price: 94
},
{
id: 10,
price: 36
},
{
id: 3,
price: 34
},
{
id: 11,
price: 71
},
];
let newArr = prices.map(function(item) {
let k = products.findIndex(elem => elem.id === item.id);
if (k !== -1) {
return products[k]
}
});
console.log(newArr)
You could sort the prices array first. Then loop through it and get the products for each id using find like this:
var products = [{id:1,name:"Buzzy"},{id:2,name:"Bytrex"},{id:3,name:"CactiDance"},{id:4,name:"CactiLoops"},{id:5,name:"Cadaver Jelly"},{id:6,name:"Caffeine Serene"},{id:7,name:"Cajun Sation"},{id:8,name:"Call it Green"},{id:9,name:"Callflex"},{id:10,name:"Calling Card Shark"},{id:11,name:"Calque"},{id:12,name:"Camel Meal Tea"},{id:13,name:"Camelot Chamomile"},{id:14,name:"Campxotica"},{id:15,name:"Camus the Killer Tale"},{id:16,name:"Candecor"},{id:17,name:"Candelarium"},{id:18,name:"CandID"},{id:19,name:"Candlelight Vittles"},{id:20,name:"Candy Ask"},{id:21,name:"Candy Floss"}],
prices = [{id:6,price:55},{id:14,price:22},{id:15,price:57},{id:4,price:41},{id:18,price:9},{id:17,price:3},{id:2,price:73},{id:7,price:43},{id:5,price:78},{id:1,price:91},{id:8,price:58},{id:16,price:69},{id:13,price:74},{id:19,price:14},{id:21,price:25},{id:12,price:84},{id:20,price:8},{id:9,price:94},{id:10,price:36},{id:3,price:34},{id:11,price:71}];
prices.sort((a, b) => a.price - b.price)
.forEach(p => {
console.log(products.find(a => a.id === p.id))
})

Searching values in array of objects /Javascript

Consider the following arrays of objects :
[{ id: 5, name: "Spain"},
{ id: 6, name: "Denmark"},
{ id: 7, name: "USA"},
{ id: 8, name: "Iceland"},
{ id: 9, name: "Greece"},
{ id: 10, name: "UK"},
{ id: 11, name: "Germany"},
{ id: 12, name: "Italy"}]
[{ id: 13, name: "US"},
{ id: 14, name: "GR"},
{ id: 15, name: "ESP"},
{ id: 16, name: "ICEL"},
{ id: 17, name: "DEN"},
{ id: 18, name: "UK"},
{ id: 19, name: "IT"},
{ id: 20, name: "GER"}]
I want to search through the first array and find the respective match for the second array. Or even better get one element from the second array and search for it against all the values in the first array .
EX. GER = { id: 11, name: "Germany"}
IT = { id: 12, name: "Italy"}
Is there a best practise for implementing this kind of search ?
If you are looking for a data structure, you could use an object with the reference to the objects of the first array.
You need to implement it by hand as long as you do not have a relation of the matching id.
countries = {
ESP: { id: 5, name: "Spain"},
DEN: { id: 6, name: "Denmark"},
US: { id: 7, name: "USA"},
ICEL: { id: 8, name: "Iceland"},
GR: { id: 9, name: "Greece"},
UK: { id: 10, name: "UK"},
GER: { id: 11, name: "Germany"},
IT: { id: 12, name: "Italy"}
}
Access via
countries.ICEL.name // Iceland
or
country = 'GR'
countries[country].name // Greece

Categories

Resources