Hướng dẫn intersection of two array of objects javascript - giao điểm của hai mảng đối tượng javascript

Tôi có hai mảng

[
    { userId: 1235, userName: 'ABC'  },
    { userId: 1236, userName: 'IJKL' }
]
6 và
[
    { userId: 1235, userName: 'ABC'  },
    { userId: 1236, userName: 'IJKL' }
]
7 có các đối tượng có một số thuộc tính;
[
    { userId: 1235, userName: 'ABC'  },
    { userId: 1236, userName: 'IJKL' }
]
8 là ID hoặc thuộc tính duy nhất:

list1 = [
    { userId: 1234, userName: 'XYZ'  }, 
    { userId: 1235, userName: 'ABC'  }, 
    { userId: 1236, userName: 'IJKL' },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
]

list2 = [
    { userId: 1235, userName: 'ABC'  },  
    { userId: 1236, userName: 'IJKL' },
    { userId: 1252, userName: 'AAAA' }
]

Tôi đang tìm kiếm một cách dễ dàng để thực hiện ba hoạt động sau:

  1. [
        { userId: 1235, userName: 'ABC'  },
        { userId: 1236, userName: 'IJKL' }
    ]
    
    9 sẽ trả lại giao điểm của các yếu tố:

    [
        { userId: 1235, userName: 'ABC'  },
        { userId: 1236, userName: 'IJKL' }
    ]
    
  2. [
        { userId: 1235, userName: 'ABC'  },
        { userId: 1236, userName: 'IJKL' }
    ]
    
    9 sẽ trả về danh sách tất cả các yếu tố từ
    [
        { userId: 1235, userName: 'ABC'  },
        { userId: 1236, userName: 'IJKL' }
    ]
    
    6 không xảy ra trong
    [
        { userId: 1235, userName: 'ABC'  },
        { userId: 1236, userName: 'IJKL' }
    ]
    
    7:

    [
        { userId: 1234, userName: 'XYZ'  },
        { userId: 1237, userName: 'WXYZ' }, 
        { userId: 1238, userName: 'LMNO' }
    ]
    
  3. [
        { userId: 1234, userName: 'XYZ'  },
        { userId: 1237, userName: 'WXYZ' }, 
        { userId: 1238, userName: 'LMNO' }
    ]
    
    3 sẽ trả về danh sách các yếu tố từ
    [
        { userId: 1235, userName: 'ABC'  },
        { userId: 1236, userName: 'IJKL' }
    ]
    
    7 không xảy ra trong
    [
        { userId: 1235, userName: 'ABC'  },
        { userId: 1236, userName: 'IJKL' }
    ]
    
    6:

    [
        { userId: 1252, userName: 'AAAA' }
    ]
    

Trincot

283K31 Huy hiệu vàng226 Huy hiệu bạc262 Huy hiệu Đồng31 gold badges226 silver badges262 bronze badges

Hỏi ngày 26 tháng 10 năm 2015 lúc 22:08Oct 26, 2015 at 22:08

3

Bạn có thể xác định ba hàm

[
    { userId: 1234, userName: 'XYZ'  },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
]
6,
[
    { userId: 1234, userName: 'XYZ'  },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
]
7 và
[
    { userId: 1234, userName: 'XYZ'  },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
]
8 tất cả đều lấy hai danh sách làm đối số và trả về một danh sách có thể được hiểu từ tên hàm. Logic chính có thể được đặt vào một chức năng chung
[
    { userId: 1234, userName: 'XYZ'  },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
]
9 mà cả ba đều dựa vào.

Dưới đây là một vài triển khai cho

[
    { userId: 1234, userName: 'XYZ'  },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
]
9 để lựa chọn, mà bạn có thể tìm thấy một đoạn trích xuống thêm:

  • JavaScript cũ đơn giản
    [
        { userId: 1252, userName: 'AAAA' }
    ]
    
    1 Loops
  • Các chức năng mũi tên bằng phương pháp mảng
    [
        { userId: 1252, userName: 'AAAA' }
    ]
    
    2 và
    [
        { userId: 1252, userName: 'AAAA' }
    ]
    
    3
  • Tra cứu được tối ưu hóa với
    [
        { userId: 1252, userName: 'AAAA' }
    ]
    
    4

Old
[
    { userId: 1252, userName: 'AAAA' }
]
1 Loops

// Generic helper function that can be used for the three operations:        
function operation[list1, list2, isUnion] {
    var result = [];
    
    for [var i = 0; i < list1.length; i++] {
        var item1 = list1[i],
            found = false;
        for [var j = 0; j < list2.length && !found; j++] {
            found = item1.userId === list2[j].userId;
        }
        if [found === !!isUnion] { // isUnion is coerced to boolean
            result.push[item1];
        }
    }
    return result;
}

// Following functions are to be used:
function inBoth[list1, list2] {
    return operation[list1, list2, true];
}

function inFirstOnly[list1, list2] {
    return operation[list1, list2];
}

function inSecondOnly[list1, list2] {
    return inFirstOnly[list2, list1];
}

// Sample data
var list1 = [
    { userId: 1234, userName: 'XYZ'  }, 
    { userId: 1235, userName: 'ABC'  }, 
    { userId: 1236, userName: 'IJKL' },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
];
var list2 = [
    { userId: 1235, userName: 'ABC'  },  
    { userId: 1236, userName: 'IJKL' },
    { userId: 1252, userName: 'AAAA' }
];
  
console.log['inBoth:', inBoth[list1, list2]]; 
console.log['inFirstOnly:', inFirstOnly[list1, list2]]; 
console.log['inSecondOnly:', inSecondOnly[list1, list2]]; 

Các chức năng mũi tên bằng phương pháp mảng
[
    { userId: 1252, userName: 'AAAA' }
]
2 và
[
    { userId: 1252, userName: 'AAAA' }
]
3

Tra cứu được tối ưu hóa với

[
    { userId: 1252, userName: 'AAAA' }
]
4

// Generic helper function that can be used for the three operations:        
const operation = [list1, list2, isUnion = false] =>
    list1.filter[ a => isUnion === list2.some[ b => a.userId === b.userId ] ];

// Following functions are to be used:
const inBoth = [list1, list2] => operation[list1, list2, true],
      inFirstOnly = operation,
      inSecondOnly = [list1, list2] => inFirstOnly[list2, list1];

// Sample data
const list1 = [
    { userId: 1234, userName: 'XYZ'  }, 
    { userId: 1235, userName: 'ABC'  }, 
    { userId: 1236, userName: 'IJKL' },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
];
const list2 = [
    { userId: 1235, userName: 'ABC'  },  
    { userId: 1236, userName: 'IJKL' },
    { userId: 1252, userName: 'AAAA' }
];
  
console.log['inBoth:', inBoth[list1, list2]]; 
console.log['inFirstOnly:', inFirstOnly[list1, list2]]; 
console.log['inSecondOnly:', inSecondOnly[list1, list2]];

Old
[
    { userId: 1252, userName: 'AAAA' }
]
1 Loops

Điều này sử dụng một số tính năng ES5 và ES6:

// Generic helper function that can be used for the three operations:        
const operation = [list1, list2, isUnion = false] =>
    list1.filter[
        [set => a => isUnion === set.has[a.userId]][new Set[list2.map[b => b.userId]]]
    ];

// Following functions are to be used:
const inBoth = [list1, list2] => operation[list1, list2, true],
      inFirstOnly = operation,
      inSecondOnly = [list1, list2] => inFirstOnly[list2, list1];

// Sample data
const list1 = [
    { userId: 1234, userName: 'XYZ'  }, 
    { userId: 1235, userName: 'ABC'  }, 
    { userId: 1236, userName: 'IJKL' },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
];
const list2 = [
    { userId: 1235, userName: 'ABC'  },  
    { userId: 1236, userName: 'IJKL' },
    { userId: 1252, userName: 'AAAA' }
];
  
console.log['inBoth:', inBoth[list1, list2]]; 
console.log['inFirstOnly:', inFirstOnly[list1, list2]]; 
console.log['inSecondOnly:', inSecondOnly[list1, list2]];

Tối ưu hóa tra cứuOct 26, 2015 at 22:32

Các giải pháp trên có độ phức tạp thời gian O [n²] vì vòng lặp lồng nhau -

[
    { userId: 1252, userName: 'AAAA' }
]
3 cũng đại diện cho một vòng lặp. Vì vậy, đối với các mảng lớn, bạn nên tạo một hàm băm [tạm thời] trên ID người dùng. Điều này có thể được thực hiện trên đường bay bằng cách cung cấp
[
    { userId: 1252, userName: 'AAAA' }
]
4 [ES6] làm đối số cho một hàm sẽ tạo ra chức năng gọi lại bộ lọc. Chức năng đó sau đó có thể thực hiện tra cứu trong thời gian không đổi với
// Generic helper function that can be used for the three operations:        
function operation[list1, list2, isUnion] {
    var result = [];
    
    for [var i = 0; i < list1.length; i++] {
        var item1 = list1[i],
            found = false;
        for [var j = 0; j < list2.length && !found; j++] {
            found = item1.userId === list2[j].userId;
        }
        if [found === !!isUnion] { // isUnion is coerced to boolean
            result.push[item1];
        }
    }
    return result;
}

// Following functions are to be used:
function inBoth[list1, list2] {
    return operation[list1, list2, true];
}

function inFirstOnly[list1, list2] {
    return operation[list1, list2];
}

function inSecondOnly[list1, list2] {
    return inFirstOnly[list2, list1];
}

// Sample data
var list1 = [
    { userId: 1234, userName: 'XYZ'  }, 
    { userId: 1235, userName: 'ABC'  }, 
    { userId: 1236, userName: 'IJKL' },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
];
var list2 = [
    { userId: 1235, userName: 'ABC'  },  
    { userId: 1236, userName: 'IJKL' },
    { userId: 1252, userName: 'AAAA' }
];
  
console.log['inBoth:', inBoth[list1, list2]]; 
console.log['inFirstOnly:', inFirstOnly[list1, list2]]; 
console.log['inSecondOnly:', inSecondOnly[list1, list2]]; 
0:trincot

283K31 Huy hiệu vàng226 Huy hiệu bạc262 Huy hiệu Đồng31 gold badges226 silver badges262 bronze badges

Hỏi ngày 26 tháng 10 năm 2015 lúc 22:08

list1.filter[a => list2.some[b => a.userId === b.userId]];  
list1.filter[a => !list2.some[b => a.userId === b.userId]];  
list2.filter[a => !list1.some[b => a.userId === b.userId]];  

Bạn có thể xác định ba hàm

[
    { userId: 1234, userName: 'XYZ'  },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
]
6,
[
    { userId: 1234, userName: 'XYZ'  },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
]
7 và
[
    { userId: 1234, userName: 'XYZ'  },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
]
8 tất cả đều lấy hai danh sách làm đối số và trả về một danh sách có thể được hiểu từ tên hàm. Logic chính có thể được đặt vào một chức năng chung
[
    { userId: 1234, userName: 'XYZ'  },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
]
9 mà cả ba đều dựa vào.

The code above will check objects by
[
    { userId: 1235, userName: 'ABC'  },
    { userId: 1236, userName: 'IJKL' }
]
8 value,
if you need complex compare rules, you can define custom comparator:

Dưới đây là một vài triển khai cho
[
    { userId: 1234, userName: 'XYZ'  },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
]
9 để lựa chọn, mà bạn có thể tìm thấy một đoạn trích xuống thêm:

JavaScript cũ đơn giản

[
    { userId: 1252, userName: 'AAAA' }
]
1 Loops
WARNING! two objects with same values will be considered different:

o1 = {"userId":1};
o2 = {"userId":2};
o1_copy = {"userId":1};
o1_ref = o1;
[o1].filter[a => [o2].includes[a]].length; // 0
[o1].filter[a => [o1_copy].includes[a]].length; // 0
[o1].filter[a => [o1_ref].includes[a]].length; // 1

Các chức năng mũi tên bằng phương pháp mảng

[
    { userId: 1252, userName: 'AAAA' }
]
2 và
[
    { userId: 1252, userName: 'AAAA' }
]
3Feb 19, 2019 at 9:47

Tra cứu được tối ưu hóa với

[
    { userId: 1252, userName: 'AAAA' }
]
4Trizalio

Old

[
    { userId: 1252, userName: 'AAAA' }
]
1 Loops6 silver badges13 bronze badges

1

Điều này sử dụng một số tính năng ES5 và ES6:

[
    { userId: 1235, userName: 'ABC'  },
    { userId: 1236, userName: 'IJKL' }
]
0

Tối ưu hóa tra cứu

[
    { userId: 1235, userName: 'ABC'  },
    { userId: 1236, userName: 'IJKL' }
]
1

Các giải pháp trên có độ phức tạp thời gian O [n²] vì vòng lặp lồng nhau -

[
    { userId: 1252, userName: 'AAAA' }
]
3 cũng đại diện cho một vòng lặp. Vì vậy, đối với các mảng lớn, bạn nên tạo một hàm băm [tạm thời] trên ID người dùng. Điều này có thể được thực hiện trên đường bay bằng cách cung cấp
[
    { userId: 1252, userName: 'AAAA' }
]
4 [ES6] làm đối số cho một hàm sẽ tạo ra chức năng gọi lại bộ lọc. Chức năng đó sau đó có thể thực hiện tra cứu trong thời gian không đổi với
// Generic helper function that can be used for the three operations:        
function operation[list1, list2, isUnion] {
    var result = [];
    
    for [var i = 0; i < list1.length; i++] {
        var item1 = list1[i],
            found = false;
        for [var j = 0; j < list2.length && !found; j++] {
            found = item1.userId === list2[j].userId;
        }
        if [found === !!isUnion] { // isUnion is coerced to boolean
            result.push[item1];
        }
    }
    return result;
}

// Following functions are to be used:
function inBoth[list1, list2] {
    return operation[list1, list2, true];
}

function inFirstOnly[list1, list2] {
    return operation[list1, list2];
}

function inSecondOnly[list1, list2] {
    return inFirstOnly[list2, list1];
}

// Sample data
var list1 = [
    { userId: 1234, userName: 'XYZ'  }, 
    { userId: 1235, userName: 'ABC'  }, 
    { userId: 1236, userName: 'IJKL' },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
];
var list2 = [
    { userId: 1235, userName: 'ABC'  },  
    { userId: 1236, userName: 'IJKL' },
    { userId: 1252, userName: 'AAAA' }
];
  
console.log['inBoth:', inBoth[list1, list2]]; 
console.log['inFirstOnly:', inFirstOnly[list1, list2]]; 
console.log['inSecondOnly:', inSecondOnly[list1, list2]]; 
0:Jul 26, 2019 at 16:47

Đã trả lời ngày 26 tháng 10 năm 2015 lúc 22:32Koushik Das

Trincottrincot3 gold badges45 silver badges39 bronze badges

câu trả lời ngắn:

[
    { userId: 1235, userName: 'ABC'  },
    { userId: 1236, userName: 'IJKL' }
]
2

Câu trả lời dài hơn: Mã trên sẽ kiểm tra các đối tượng bằng giá trị

[
    { userId: 1235, userName: 'ABC'  },
    { userId: 1236, userName: 'IJKL' }
]
8, nếu bạn cần so sánh phức tạp, bạn có thể xác định bộ so sánh tùy chỉnh:

________số 8Oct 26, 2015 at 22:15

Ngoài ra, có một cách để so sánh các đối tượng bằng tài liệu tham khảo! Hai đối tượng có cùng giá trị sẽ được coi là khác nhau:Bwaxxlo

Đã trả lời ngày 19 tháng 2 năm 2019 lúc 9:4713 silver badges18 bronze badges

[
    { userId: 1235, userName: 'ABC'  },
    { userId: 1236, userName: 'IJKL' }
]
3

TrizaliotrizalioOct 26, 2015 at 22:40

7316 Huy hiệu bạc13 Huy hiệu đồngSagi

Chỉ cần sử dụng các phương thức mảng

[
    { userId: 1252, userName: 'AAAA' }
]
2 và
[
    { userId: 1252, userName: 'AAAA' }
]
3 của JS và bạn có thể làm điều đó.3 gold badges33 silver badges40 bronze badges

Điều này sẽ trả về các mục có mặt trong

[
    { userId: 1235, userName: 'ABC'  },
    { userId: 1236, userName: 'IJKL' }
]
6 nhưng không phải trong
[
    { userId: 1235, userName: 'ABC'  },
    { userId: 1236, userName: 'IJKL' }
]
7. Nếu bạn đang tìm kiếm các mục phổ biến trong cả hai danh sách. Chỉ cần làm điều này.

[
    { userId: 1235, userName: 'ABC'  },
    { userId: 1236, userName: 'IJKL' }
]
4

Đã trả lời ngày 26 tháng 7 năm 2019 lúc 16:47

Koushik Daskoushik DasOct 26, 2015 at 22:38

8.1943 Huy hiệu vàng45 Huy hiệu bạc39 Huy hiệu Đồnglaruiss

Sử dụng phương pháp

// Generic helper function that can be used for the three operations:        
function operation[list1, list2, isUnion] {
    var result = [];
    
    for [var i = 0; i < list1.length; i++] {
        var item1 = list1[i],
            found = false;
        for [var j = 0; j < list2.length && !found; j++] {
            found = item1.userId === list2[j].userId;
        }
        if [found === !!isUnion] { // isUnion is coerced to boolean
            result.push[item1];
        }
    }
    return result;
}

// Following functions are to be used:
function inBoth[list1, list2] {
    return operation[list1, list2, true];
}

function inFirstOnly[list1, list2] {
    return operation[list1, list2];
}

function inSecondOnly[list1, list2] {
    return inFirstOnly[list2, list1];
}

// Sample data
var list1 = [
    { userId: 1234, userName: 'XYZ'  }, 
    { userId: 1235, userName: 'ABC'  }, 
    { userId: 1236, userName: 'IJKL' },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
];
var list2 = [
    { userId: 1235, userName: 'ABC'  },  
    { userId: 1236, userName: 'IJKL' },
    { userId: 1252, userName: 'AAAA' }
];
  
console.log['inBoth:', inBoth[list1, list2]]; 
console.log['inFirstOnly:', inFirstOnly[list1, list2]]; 
console.log['inSecondOnly:', inSecondOnly[list1, list2]]; 
6 của Lodash. Đặc biệt:1 gold badge16 silver badges29 bronze badges

1

Trên đây cung cấp cho bạn tương đương với

// Generic helper function that can be used for the three operations:        
function operation[list1, list2, isUnion] {
    var result = [];
    
    for [var i = 0; i < list1.length; i++] {
        var item1 = list1[i],
            found = false;
        for [var j = 0; j < list2.length && !found; j++] {
            found = item1.userId === list2[j].userId;
        }
        if [found === !!isUnion] { // isUnion is coerced to boolean
            result.push[item1];
        }
    }
    return result;
}

// Following functions are to be used:
function inBoth[list1, list2] {
    return operation[list1, list2, true];
}

function inFirstOnly[list1, list2] {
    return operation[list1, list2];
}

function inSecondOnly[list1, list2] {
    return inFirstOnly[list2, list1];
}

// Sample data
var list1 = [
    { userId: 1234, userName: 'XYZ'  }, 
    { userId: 1235, userName: 'ABC'  }, 
    { userId: 1236, userName: 'IJKL' },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
];
var list2 = [
    { userId: 1235, userName: 'ABC'  },  
    { userId: 1236, userName: 'IJKL' },
    { userId: 1252, userName: 'AAAA' }
];
  
console.log['inBoth:', inBoth[list1, list2]]; 
console.log['inFirstOnly:', inFirstOnly[list1, list2]]; 
console.log['inSecondOnly:', inSecondOnly[list1, list2]]; 
7 [theo thuật ngữ SQL,
// Generic helper function that can be used for the three operations:        
function operation[list1, list2, isUnion] {
    var result = [];
    
    for [var i = 0; i < list1.length; i++] {
        var item1 = list1[i],
            found = false;
        for [var j = 0; j < list2.length && !found; j++] {
            found = item1.userId === list2[j].userId;
        }
        if [found === !!isUnion] { // isUnion is coerced to boolean
            result.push[item1];
        }
    }
    return result;
}

// Following functions are to be used:
function inBoth[list1, list2] {
    return operation[list1, list2, true];
}

function inFirstOnly[list1, list2] {
    return operation[list1, list2];
}

function inSecondOnly[list1, list2] {
    return inFirstOnly[list2, list1];
}

// Sample data
var list1 = [
    { userId: 1234, userName: 'XYZ'  }, 
    { userId: 1235, userName: 'ABC'  }, 
    { userId: 1236, userName: 'IJKL' },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
];
var list2 = [
    { userId: 1235, userName: 'ABC'  },  
    { userId: 1236, userName: 'IJKL' },
    { userId: 1252, userName: 'AAAA' }
];
  
console.log['inBoth:', inBoth[list1, list2]]; 
console.log['inFirstOnly:', inFirstOnly[list1, list2]]; 
console.log['inSecondOnly:', inSecondOnly[list1, list2]]; 
8]. Bạn có thể di chuyển mã xung quanh mã để có được những gì bạn muốn!

[
    { userId: 1235, userName: 'ABC'  },
    { userId: 1236, userName: 'IJKL' }
]
5

Đã trả lời ngày 26 tháng 10 năm 2015 lúc 22:15Oct 30, 2015 at 15:18

BWAXXLOBWAXXXLOShashi

1.86013 Huy hiệu bạc18 Huy hiệu đồng2 gold badges17 silver badges32 bronze badges

Chủ Đề