简体   繁体   English

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

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

I've seen lots of similar questions, but nothing that seems to exactly match what I'm trying to do.我见过很多类似的问题,但似乎没有什么与我正在尝试做的完全匹配。 I was able to get the code working, but I feel it needs to be refactored and I'm not sure how to do this?我能够让代码正常工作,但我觉得它需要重构,但我不确定该怎么做?

I have two for in loops that goes through the JSON to get each of the objects that I call item.我有两个 for in 循环遍历 JSON 以获取我称为项目的每个对象。 That worked, but I was unable to do another loop or figure out a way to get one of the values of the item based on a specific property.这行得通,但我无法执行另一个循环或想出一种方法来根据特定属性获取项目的值之一。 So I created a function from some code I found here: Javascript: find an object in an array based on the object's property所以我从我在这里找到的一些代码创建了一个函数: Javascript: find an object in an array based on the object's property

My question is, is this the proper way to code this or is there a better way by another nested loop somehow, which is what I was trying originally?我的问题是,这是编写此代码的正确方法,还是通过某种方式通过另一个嵌套循环有更好的方法,这是我最初尝试的方法?

Here is the working Plunker这是工作的Plunker

Code:代码:

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;
      }
});

Here is the console output这是控制台输出

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"

This is the data I wanted to select.这是我要选择的数据。

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

objItem name is: "Plan B" objItem 名称是:“Plan B”

objItem value is: [497] objItem 值为:[497]

objItem displayed is: true显示的 objItem 是:true

  • Assuming you have only one GROUP , you can use the function find along with the function some as follow.假设您只有一个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; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM