最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Filtering two json arrays with properties - Stack Overflow

matteradmin6PV0评论

i have to filter first array if the same object is present in second array using type script/ javascript

here is my arrays

var students = [{id: 1, name : 'SSS'},
{id: 2, name : 'SSa'},
{id: 3, name : 'SSb'},
{id: 4, name : 'SSc'},
{id: 5, name : 'SSd'}];

var emp = [{id: 1, name : 'SSS'},
{id: 4, name : 'SSc'},
{id: 5, name : 'SSd'}];

Output should be

var finalarr = [{id: 2, name : 'SSa'},
{id: 3, name : 'SSb'}]

i have tried below but produces wrong ouputs

for (let i = 0; i < students.length; i++) {
    for (let j = 0; j < emp.length; j++) {
    if (students[i].id != emp[j].id) {
    finalarr.push(students[i]);
    }
}
}
console.log(finalarr);

// below is my actual application code

this.appointmentTypes.filter((data, index) => this.typesData.includes(this.typesData[index].appointmentType.id))

i have to filter first array if the same object is present in second array using type script/ javascript

here is my arrays

var students = [{id: 1, name : 'SSS'},
{id: 2, name : 'SSa'},
{id: 3, name : 'SSb'},
{id: 4, name : 'SSc'},
{id: 5, name : 'SSd'}];

var emp = [{id: 1, name : 'SSS'},
{id: 4, name : 'SSc'},
{id: 5, name : 'SSd'}];

Output should be

var finalarr = [{id: 2, name : 'SSa'},
{id: 3, name : 'SSb'}]

i have tried below but produces wrong ouputs

for (let i = 0; i < students.length; i++) {
    for (let j = 0; j < emp.length; j++) {
    if (students[i].id != emp[j].id) {
    finalarr.push(students[i]);
    }
}
}
console.log(finalarr);

// below is my actual application code

this.appointmentTypes.filter((data, index) => this.typesData.includes(this.typesData[index].appointmentType.id))
Share Improve this question asked May 28, 2018 at 11:08 SanthoshSanthosh 1,0892 gold badges20 silver badges52 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Objects are references to memory locations - they'll never === each other unless they reference the same memory location, so includes won't work. Iterate over the whole array and test the names and IDs instead:

var students=[{id:1,name:'SSS'},{id:2,name:'SSa'},{id:3,name:'SSb'},{id:4,name:'SSc'},{id:5,name:'SSd'}]
var emp=[{id:1,name:'SSS'},{id:4,name:'SSc'},{id:5,name:'SSd'}]

const finalArr = students.filter(({ id, name }) =>
  !emp.some(exclude => exclude.id === id && exclude.name === name)
);
console.log(finalArr);

Your imperative attempt w/ loops fails because you push to final array when finding the first item with a different id.

It should be like

var students = [{id: 1, name : 'SSS'},
{id: 2, name : 'SSa'},
{id: 3, name : 'SSb'},
{id: 4, name : 'SSc'},
{id: 5, name : 'SSd'}];

var emp = [{id: 1, name : 'SSS'},
{id: 4, name : 'SSc'},
{id: 5, name : 'SSd'}];

var finalarr = []

for (let i = 0; i < students.length; i++) {
    let found = false // flag
    
    for (let j = 0; j < emp.length && !found; j++) {
      found = students[i].id === emp[j].id
    }
    
    if (!found) finalarr.push(students[i])
}

console.log(finalarr)

But a better option would be to create a Set of ids from the second array. new Set(emp.map(item => item.id)) And the simply filter out all elements of the first array which ids are in the set using filter(item => !set.has(item.id))

var students = [{id: 1, name : 'SSS'},
{id: 2, name : 'SSa'},
{id: 3, name : 'SSb'},
{id: 4, name : 'SSc'},
{id: 5, name : 'SSd'}];

var emp = [{id: 1, name : 'SSS'},
{id: 4, name : 'SSc'},
{id: 5, name : 'SSd'}];

console.log(
  students.filter(
    (set => item => !set.has(item.id))(new Set(emp.map(item => item.id)))
  )
)

Set and Map tend to be great for solving filtration problems.

See below for more info

Post a comment

comment list (0)

  1. No comments so far