How to check if an object exist in 2 arrays in angular

If you have an array that must be filtered based on anther array value.

The list that must be filtered

[
  { name: 'Anna', age: 23, skills: [{name: 'a', desc: 'aaa'}] },
  { name: 'Barbara', age: 26, skills: [{name: 'a', desc: 'aaa'}, {name: 'b', desc: 'bbb'} ] }
]

input for comparison/filter

   [{name: 'b', desc: 'bbb'}]

function called with input [{name: ‘b’, dec: ‘bbb’}]

updatePersonList([{name: 'b', dec: 'bbb'}]);

updatePeronList(skill: {name: string, desc: string}) {
this.filteredList = this.filteredList.filter(person => {
  person?.skills(skills => {
    // I have tried include, some and some other things on I think its because you cannot compare objects like that that must be done with .equels or some other way.
  });
});
}

What I like to to is if there is any object in the given array that is also available in the skill array than this person must be shown in the filtered list.

If there is no match at al the person must not be shown.

Maybe its’ still unclear please ask a question and I will try the explain in another way

1

To simplify the code, you can avoid the nested .some() method by using a more straightforward approach, like combining the lookup objects in a Set or filtering directly with a cleaner structure. Here’s a more concise version of your code:

const arr = [
  { name: 'Anna', age: 23, skills: [{name: 'a', desc: 'aaa'}] },
  { name: 'Barbara', age: 26, skills: [{name: 'a', desc: 'aaa'}, {name: 'b', desc: 'bbb'} ] }
];

const lookup = [{name: 'b', desc: 'bbb'}];

function getFiltered(lookup) {
  const lookupSet = new Set(lookup.map(item => JSON.stringify(item)));
  console.log(lookupSet);
  return arr.filter(item =>
    item.skills.some(skill => lookupSet.has(JSON.stringify(skill)))
  );
}

console.log(getFiltered(lookup));

2

so if I understood correctly, you are trying to filter each object based on their skills. You are working on three data types: an array of objects, and inside each object you have another array of objects which you want to filter. To achieve the desired result, I would suggest the following:

function filterBySkills(inputForComparison) {
    return unfilteredArray.filter(person => {
        return person.skills.find(skill => {
            return JSON.stringify(skill) === JSON.stringify(inputForComparison);
        });
    });
}

where inputForComparison = {name: 'b', desc: 'bbb'} . I have chosen turning the objects into strings to compare them, this can also be done using lodash _.isEqual() as mentioned here. Hope this can help you solve the issue. 🙂

From what I understand, you want to filter a list of persons based on whether they have any skills that match a given set of skills to filter.

Use a combination of filter() and some() to check for skill matches:

updatePersonList(skillsToFilter: {name: string, desc: string}[]): void {
  this.filteredList = this.originalList.filter(person => 
    person.skills.some(personSkill => 
      skillsToFilter.some(filterSkill => 
        this.areSkillsEqual(personSkill, filterSkill)
      )
    )
  );
}

// Helper method to compare skills precisely
private areSkillsEqual(
  skill1: {name: string, desc: string}, 
  skill2: {name: string, desc: string}
): boolean {
  return skill1.name === skill2.name && skill1.desc === skill2.desc;
}

Example Usage:

riginalList = [
  { name: 'Anna', skills: [{name: 'a', desc: 'aaa'}] },
  { name: 'Barbara', skills: [{name: 'a', desc: 'aaa'}, {name: 'b', desc: 'bbb'}] }
];

// Filter for persons with skill 'b'
const skillsToFilter = [{name: 'b', desc: 'bbb'}];
this.updatePersonList(skillsToFilter);

Hope this helps?
Let me know if you have any questions…

New contributor

Ajayi Temitope is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật