嵌套在对象迭代中的对象?

时间:2016-12-22 00:26:40

标签: javascript

我想编写一个函数,它将找到Categoy [3] .options [3] .label并将其与字母“#34; D"”匹配。

如何遍历下面的嵌套对象以浏览TagCategory.options并搜索与字母匹配的选项" D"?如果它匹配字母" D",它应该返回true。

"Category": 
[
  {
      "field": "A", 
      "options": [
          {"tag": "100", "value": "yes"}
      ], 
      "label": "Red"
  }, 

  {
      "field": "tanks", 
      "type": true, 
      "options": [
          {"tag_value": "4", "value": "4", "label": "A"}, 
          {"tag_value": "3", "value": "3", "label": "B"}, 
          {"tag_value": "2", "value": "2", "label": "C"}, 
          {"tag_value": "1", "value": "1", "label": "D"}, 
          {"tag_value": "5", "value": "5", "label": "E"}
      ], 
      "label": "Tanks"
  }
]

4 个答案:

答案 0 :(得分:0)

这样的东西?

function findLabel(arr, label) {

  for (var i in arr) {
    const options = arr[i].options;
    const find = options.find(o => o.label == label);
    if (find) {
      return true;
    }
  }

  return false;

}

const test = findLabel(Category, "D");

类别是你的数组

答案 1 :(得分:0)

function check(category, letter){
  var obj;
  var optionObj;
  for(var i = 0; i < category.length; i++){
    obj = Category[i];
      options = obj.options;
      for(var j = 0; j < options.length; j++){
        optionObj = option[j];
        if(optionObj.label === letter) return true;
      }
  }
  return false;
}

答案 2 :(得分:0)

您可以使用Array.prototype.some()

let optionHasLabelDBool = obj.Category.some(({options}) =>   
                            options.some(({label}) => label === "D"));

&#13;
&#13;
let obj = {
    "Category":
      [{
          "field": "A",
          "options": [{
            "tag": "100",
            "value": "yes"
          }],
          "label": "Red"
        },

        {
          "field": "tanks",
          "type": true,
          "options": [{
            "tag_value": "4",
            "value": "4",
            "label": "A"
          }, {
            "tag_value": "3",
            "value": "3",
            "label": "B"
          }, {
            "tag_value": "2",
            "value": "2",
            "label": "C"
          }, {
            "tag_value": "1",
            "value": "1",
            "label": "D"
          }, {
            "tag_value": "5",
            "value": "5",
            "label": "E"
          }],
          "label": "Tanks"
        }
      ]
    };
  
let optionHasLabelD = obj.Category.some(({options}) => options.some(({label}) => label === "D"));

console.log(optionHasLabelD);
&#13;
&#13;
&#13;

使用功能,无箭头功能或对象破坏

function checkOption(array, prop, key, value) {
  return array.some(function(obj) {
    return obj[prop].some(function(match) {
      return match[key] === value;
    });
  });
}

let optionHasLabelDBool = checkOption(obj.Category, "options", "label", "D");

&#13;
&#13;
let obj = {
  "Category": [{
      "field": "A",
      "options": [{
        "tag": "100",
        "value": "yes"
      }],
      "label": "Red"
    },

    {
      "field": "tanks",
      "type": true,
      "options": [{
        "tag_value": "4",
        "value": "4",
        "label": "A"
      }, {
        "tag_value": "3",
        "value": "3",
        "label": "B"
      }, {
        "tag_value": "2",
        "value": "2",
        "label": "C"
      }, {
        "tag_value": "1",
        "value": "1",
        "label": "D"
      }, {
        "tag_value": "5",
        "value": "5",
        "label": "E"
      }],
      "label": "Tanks"
    }
  ]
};


function checkOption(array, prop, key, value) {
  return array.some(function(obj) {
    return obj[prop].some(function(match) {
      console.log(match[key], value, match[key] === value);
      return match[key] === value;
    });
  });
}

let optionHasLabelDBool = checkOption(obj.Category, "options", "label", "D");

console.log(optionHasLabelDBool);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

以下是嵌套for循环的一个粗略示例。

category = {...}; // your object above
for (i in category) {
    field = category[i];
    for (option_i in field.options) {  
        // This is the nested for loop - called thus because it's inside one.
        if (field.options[option_i].label == "D"){
            return true;
        }
    }
}