Javascript 将一个数组值与另一个数组值进行比较

Javascript 将一个数组值与另一个数组值进行比较,javascript,arrays,angularjs,Javascript,Arrays,Angularjs,我有一个数组,其值如下: userID: ["55f6c3639e3cdc00273b57a5", "55f6c36e9e3cdc00273b57a6", "55f6c34e9e3cdc00273b57a3"]; $scope.userList : [Object, Object, Object, Object, Object], 其中每个对象都有一个我正在比较的ID属性 我想比较userList数组中是否存在eachuserID数组值 $scope.userInfo =

我有一个数组,其值如下:

userID: ["55f6c3639e3cdc00273b57a5", 
        "55f6c36e9e3cdc00273b57a6", "55f6c34e9e3cdc00273b57a3"];

$scope.userList : [Object, Object, Object, Object, Object], 
其中每个对象都有一个我正在比较的ID属性

我想比较userList数组中是否存在each
userID
数组值

$scope.userInfo = function(userID) {
    var userDetails = [];
    for (var i = 0; i < $scope.userList.length; i++) {
        (function(i) {
            for (var j = i; j < userID.length; j++) {
                if ($scope.userList[i]._id === userID[j]) {
                    userDetails.push($scope.userList[i]);
                }
            }
        })(i)
    }
    return userDetails;
};
$scope.userInfo=函数(userID){
var userDetails=[];
对于(变量i=0;i<$scope.userList.length;i++){
(职能(一){
for(var j=i;j
我面临的问题是,对于数组中的每个
userID
,我想将其与
userList
对象中要匹配的所有项进行比较

上述代码不起作用。它不会将每个数组值与整个对象进行比较。

试试这个

$scope.userInfo = function(userID) {
        var userDetails = [];
        for (var i = 0; i < $scope.userList.length; i++) {       
                for (var j = 0; j < userID.length; j++) {
                    if ($scope.userList[i]._id === userID[j]) {
                        userDetails.push(userID[j]);
                    }
                }       
        }
        return userDetails;
    };

试试这个

$scope.userInfo = function(userID) {
        var userDetails = [];
        for (var i = 0; i < $scope.userList.length; i++) {       
                for (var j = 0; j < userID.length; j++) {
                    if ($scope.userList[i]._id === userID[j]) {
                        userDetails.push(userID[j]);
                    }
                }       
        }
        return userDetails;
    };

试试这个

$scope.userInfo = function(userID) {
        var userDetails = [];
        for (var i = 0; i < $scope.userList.length; i++) {       
                for (var j = 0; j < userID.length; j++) {
                    if ($scope.userList[i]._id === userID[j]) {
                        userDetails.push(userID[j]);
                    }
                }       
        }
        return userDetails;
    };

试试这个

$scope.userInfo = function(userID) {
        var userDetails = [];
        for (var i = 0; i < $scope.userList.length; i++) {       
                for (var j = 0; j < userID.length; j++) {
                    if ($scope.userList[i]._id === userID[j]) {
                        userDetails.push(userID[j]);
                    }
                }       
        }
        return userDetails;
    };

你应该试试使用

JS:

您应该尝试使用

JS:

您应该尝试使用

JS:

您应该尝试使用

JS:


$scope.userList
转换为以
用户ID
为键的对象,而不是使用两个嵌套循环。然后,您可以循环使用
userID
数组,快速检查新对象中是否存在具有相同密钥的用户

通过删除嵌套循环,下面的代码将以线性时间运行,而不是以n^2的时间运行,如果您有大型数组,这将非常有益。如果将
$scope.userList
存储为一个对象,该对象由其
用户ID
设置关键字,则无需在每次运行函数时创建索引,即可节省更多时间

$scope.userInfo = function(userID) {

    var userList = {};

    //create object keyed by user_id
    for(var i=0;i<$scope.userList.length;i++) {
        userList[$scope.userList._id] = $scope.userList;
    }

    //now for each item in userID, see if an element exists
    //with the same key in userList created above

    var userDetails = [];
    for(i=0;i<userID.length;i++) {
        if(userID[i] in userList) {
            userDetails.push(userList[userID[i]]);
        }
    }

    return userDetails;
};
$scope.userInfo=函数(userID){
var userList={};
//创建由用户\u id设置关键帧的对象

对于(var i=0;i而不是使用两个嵌套循环,将
$scope.userList
转换为一个以
userID
为键的对象。然后,您可以通过
userID
数组进行循环,并快速检查新对象中是否存在具有相同键的用户

通过删除嵌套循环,下面的代码以线性时间而不是n^2运行,如果您有大型数组,这将是有益的。如果您将
$scope.userList
存储为一个由其
userId
设置关键帧的对象,则无需在每次运行函数时创建索引,就可以节省更多时间

$scope.userInfo = function(userID) {

    var userList = {};

    //create object keyed by user_id
    for(var i=0;i<$scope.userList.length;i++) {
        userList[$scope.userList._id] = $scope.userList;
    }

    //now for each item in userID, see if an element exists
    //with the same key in userList created above

    var userDetails = [];
    for(i=0;i<userID.length;i++) {
        if(userID[i] in userList) {
            userDetails.push(userList[userID[i]]);
        }
    }

    return userDetails;
};
$scope.userInfo=函数(userID){
var userList={};
//创建由用户\u id设置关键帧的对象

对于(var i=0;i而不是使用两个嵌套循环,将
$scope.userList
转换为一个以
userID
为键的对象。然后,您可以通过
userID
数组进行循环,并快速检查新对象中是否存在具有相同键的用户

通过删除嵌套循环,下面的代码以线性时间而不是n^2运行,如果您有大型数组,这将是有益的。如果您将
$scope.userList
存储为一个由其
userId
设置关键帧的对象,则无需在每次运行函数时创建索引,就可以节省更多时间

$scope.userInfo = function(userID) {

    var userList = {};

    //create object keyed by user_id
    for(var i=0;i<$scope.userList.length;i++) {
        userList[$scope.userList._id] = $scope.userList;
    }

    //now for each item in userID, see if an element exists
    //with the same key in userList created above

    var userDetails = [];
    for(i=0;i<userID.length;i++) {
        if(userID[i] in userList) {
            userDetails.push(userList[userID[i]]);
        }
    }

    return userDetails;
};
$scope.userInfo=函数(userID){
var userList={};
//创建由用户\u id设置关键帧的对象

对于(var i=0;i而不是使用两个嵌套循环,将
$scope.userList
转换为一个以
userID
为键的对象。然后,您可以通过
userID
数组进行循环,并快速检查新对象中是否存在具有相同键的用户

通过删除嵌套循环,下面的代码以线性时间而不是n^2运行,如果您有大型数组,这将是有益的。如果您将
$scope.userList
存储为一个由其
userId
设置关键帧的对象,则无需在每次运行函数时创建索引,就可以节省更多时间

$scope.userInfo = function(userID) {

    var userList = {};

    //create object keyed by user_id
    for(var i=0;i<$scope.userList.length;i++) {
        userList[$scope.userList._id] = $scope.userList;
    }

    //now for each item in userID, see if an element exists
    //with the same key in userList created above

    var userDetails = [];
    for(i=0;i<userID.length;i++) {
        if(userID[i] in userList) {
            userDetails.push(userList[userID[i]]);
        }
    }

    return userDetails;
};
$scope.userInfo=函数(userID){
var userList={};
//创建由用户\u id设置关键帧的对象

对于(var i=0;i您已经在这样做了。那么问题是什么呢?内环只是从外环位置开始(
for(var j=i;…
)-我怀疑这是你想要的,我不能在If的中间看到任何原因。你已经在做了。那么问题是什么?内环只是从外环位置开始的(<代码>为(var j= i;…< /代码>)。-我怀疑这是你想要的,我不能在If的中间看到任何原因。你已经在做了。那么问题是什么?内环只是从外环位置开始的(<代码>为(var j= i;…< /代码>)。-我怀疑这是你想要的,我不能在If的中间看到任何原因。你已经在做了。那么问题是什么?内环只是从外环位置开始的(<代码>为(var j= i;…< /代码>)。-我怀疑这是你想要做的,我看不出有任何理由的IIFE在这个循环的中间。