如何在 JSON 对象数组中查找并匹配特定值?

How to find and match a specific value in a JSON object array?

我见过很多类似的问题,但似乎没有一个与我正在尝试做的完全匹配。我能够让代码工作,但是 我觉得它需要重构,但我不确定该怎么做?

我有两个 for in 循环通过 JSON 来获取我称为 item 的每个对象。这行得通,但我无法执行另一个循环或想出一种方法来根据特定 属性 获取项目的值之一。所以我根据在这里找到的一些代码创建了一个函数:Javascript: find an object in an array based on the object's property

我的问题是,这是编写代码的正确方法吗? 以某种方式通过另一个嵌套循环获得更好的方法,这是我最初尝试的方法?

这是工作 Plunker

代码:

var app = angular.module('myApp', []);

app.controller('MainCtrl', function($scope) {
  $scope.items = {"GROUP":[
            {"name":"Plan A","value":[163],"displayed":true},
            {"name":"Plan B","value":[497],"displayed":true},
            {"name":"Plan C","value":[324],"displayed":true},
            {"name":"Plan D","value":[476],"displayed":true},
            {"name":"Plan E","value":[343],"displayed":true}]};

     for (var key in $scope.items) {
       console.log("key is: " + key + " " + JSON.stringify($scope.items[key]));

        var item = $scope.items[key];
        for(var itemKey in item){
          //item is part of an object array
          console.log("item is an array: " + JSON.stringify(item)); 

          //itemKey is the number
          console.log("itemKey is: " + itemKey); 

          //Each item in the GROUP
          console.log("Each item: " + JSON.stringify(item[itemKey]));

          //The names in the items.
          console.log("The item name is: " + JSON.stringify(item[itemKey].name));

        }
        var objItem = findObjectByKey(item, 'name', 'Plan B');
        console.log("objItem is: " + JSON.stringify(objItem));
        console.log("objItem name is: " + JSON.stringify(objItem.name));
        console.log("objItem value is: " + JSON.stringify(objItem.value));
        console.log("objItem displayed is: " + JSON.stringify(objItem.displayed));
      }

      function findObjectByKey(array, key, value) {
          for (var i = 0; i < array.length; i++) {
              if (array[i][key] === value) {
                  return array[i];
              }
          }
          return null;
      }
});

这是控制台输出

key is: GROUP [
{"name":"Plan A","value":[163],"displayed":true},
{"name":"Plan B","value":[497],"displayed":true},
{"name":"Plan C","value":[324],"displayed":true},
{"name":"Plan D","value":[476],"displayed":true},
{"name":"Plan E","value":[343],"displayed":true}]

itemKey is: 0
itemKey is: 1
itemKey is: 2
itemKey is: 3
itemKey is: 4
itemKey is: 5

The item name is:  "Plan A"
The item name is:  "Plan B"
The item name is:  "Plan C"
The item name is:  "Plan D"
The item name is:  "Plan E"

这是我要的数据select.

objItem 是:{ "name":"Plan B","value":[497],"displayed":true }

objItem 名称是:"Plan B"

objItem 值为:[497]

objItem 显示为:true

  • 假设您只有一个 GROUP,您可以使用函数 find 和函数 some,如下所示。

let items = {"GROUP":[            {"name":"Plan A","value":[163],"displayed":true},            {"name":"Plan B","value":[497],"displayed":true},            {"name":"Plan C","value":[324],"displayed":true},            {"name":"Plan D","value":[476],"displayed":true},            {"name":"Plan E","value":[343],"displayed":true}]},
    key = 'Plan B',
    object = items.GROUP.find(o => Object.entries(o).some(([k, value]) => k === 'name' && value === key));

console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }