javascript - Filtering two json arrays with properties - Stack Overflow

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745598976a4635286.html

相关推荐

  • javascript - Filtering two json arrays with properties - Stack Overflow

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

    16小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信