【问题标题】:Search array for specific string and return the value to AngularJS view搜索特定字符串的数组并将值返回到 AngularJS 视图
【发布时间】:2017-05-10 23:43:56
【问题描述】:

在下面的代码中,我尝试在 JSON 输出中搜索特定字符串(例如“name_col_lbl”)并将其值(在本例中为“Name”)返回给 AngularJS 以在视图中输出。

$scope.globalContent = [
    {"id":"1","module":"student_reg","item":"name_col_lbl","value":"Name"},
    {"id":"2","module":"student_reg","item":"bday_col_lbl","value":"Birthdate"}
]
angular.forEach($scope.globalContent, function(el){
    $scope.nameLbl = el ***This is where I need to search for the string and return its value***;
    $scope.bdayLbl= el ***This is where I need to search for the string and return its value***;

});

我似乎找不到有效的方法来处理这个问题。提前致谢!

【问题讨论】:

    标签: javascript angularjs arrays json


    【解决方案1】:

    这应该可以解决问题:

    var $scopeglobalContent = [
        {"id":"1","module":"student_reg","item":"name_col_lbl","value":"Name"},
        {"id":"2","module":"student_reg","item":"bday_col_lbl","value":"Birthdate"}
    ];
    
    for(var i = 0; i < $scopeglobalContent.length; i++){
    	for(key in $scopeglobalContent[i]){
      	if($scopeglobalContent[i][key] == "name_col_lbl"){
        	return console.log($scopeglobalContent[i].value);
        }
      }
    }

    这是基本的东西。我建议您阅读 objectsloops 以更好地了解它的工作原理和使用方法。

    【讨论】:

    • 谢谢。我是自学成才,确实缺少一些非常基本的知识。我很珍惜每一个学习机会。
    • @spookybot 我也是自学成才的,我学习的方式是阅读和编写更多代码并尝试不同的概念和技术。
    • 另外,为了让看到这篇文章的人清楚,它应该在你的代码中使用 $scope.globalContent。使用 AngularJS,您需要 $scope 之后的句点。
    • @spookybot 我删除了 $scope. 因为我在 jsfiddle 中运行它
    • 酷我感谢您的指导。现在阅读有关对象和循环的更多信息。
    【解决方案2】:

    来自this stackoverflow answer,以下是搜索对象数组的方法:

    var obj = array.filter(function ( obj ) {
        return obj.item === "name_col_lbl";
    });
    

    obj 将包含 name_col_lblundefined 的值(如果键不存在)。

    所以你的代码应该是这样的:

    function findObject(array, keyName) {
        var obj = array.filter(function ( obj ) {
            return obj.item === keyName;
        })[0];
        return obj;
    }
    
    $scope.globalContent = [
        {"id":"1","module":"student_reg","item":"name_col_lbl","value":"Name"},
        {"id":"2","module":"student_reg","item":"bday_col_lbl","value":"Birthdate"}
    ]
    angular.forEach($scope.globalContent, function(el){
        $scope.nameLbl = findObject($scope.globalContent, "name_col_lbl")['value]' ***This is where I need to search for the string and return its value***;
        $scope.bdayLbl= findObject($scope.globalContent, "bday_col_lbl")['value]' ***This is where I need to search for the string and return its value***;
    });
    

    findObject 函数通过返回它找到的第一个对象来过滤数组,其中obj.item 匹配keyName 中包含的任何内容。这是返回的 obj 对象。由于您不想要整个对象,而只想要该对象的值,因此我获取了 findObject($scope.globalContent, "name_col_lbl") 的结果并添加了 ['value'] ,它将仅返回您想要显示的 value 键Angular 视图。

    【讨论】:

    • 我喜欢你的方法 Dr. Cool 你觉得与@Josan_Iracheta 的回答相比,这是一种更有效和/或更清洁的方法吗?
    • 其实我不确定这是否能实现我的目标。这不会返回与匹配字符串的对象关联的“值”字符串的值。
    • 我更新了代码以根据需要提取“值”字符串...现在试试。另外,我喜欢乔桑的回答。我认为这两个都很好,它们是让你看到完成同一件事的两种不同方法的好方法。至于性能,我不确定哪个更快,但您没有搜索大量数据(我假设),所以我认为您可以使用任何一种方法。
    • 非常感谢 Cool 博士 -- 这对我来说是一个很好的学习机会。
    • @Dr.Cool 你的代码在这个例子中不起作用,因为array 必须是$scope.globalContent。其次,您只搜索数组中的第一个对象。如果该函数无需显式声明要搜索的索引就可以搜索数组会更好,这就是我的代码所做的。
    猜你喜欢
    • 2011-04-23
    • 1970-01-01
    • 2014-07-26
    • 2018-06-24
    • 1970-01-01
    • 2021-01-19
    • 2013-03-02
    • 2021-09-27
    • 1970-01-01
    相关资源
    最近更新 更多