Compare two multidimensional arrays in javascript

后端 未结 4 856
故里飘歌
故里飘歌 2021-01-23 09:18

I have two arrays:

var array_old = [{id:\"5436\", title:\"I Like you boy\"}, {id:\"5437\", title:\"Hello how are you\"}];
var array_new = [{id:\"5436\", title:\"         


        
4条回答
  •  灰色年华
    2021-01-23 09:43

    Depends on how big your arrays are - you might want to use a more performant solution.

    • The easiest solution (which both you and @Tebb found) has Θ(n*m)
    • If you would optimize this a bit (breaking out if you did [not] found the element - see @gonchuki), you are still at O(n*m)
    • You could assume that both arrays are in the same order, and run only one loop: O(min(n,m)). If you'd need to sort them before that, you'd get O(n*log n+m*log m).
    • Best would be using a hash table for O(1) lookup, resulting in O(n+m). You can easily use a JS object for that:
    var counts = {};
    for (var i=0; i

    (Demo)

提交回复
热议问题