【问题标题】:Looping through an object to get specific values循环遍历对象以获取特定值
【发布时间】:2018-05-03 11:44:04
【问题描述】:

我有一个混合了对象和非对象的以下对象。

{
  "boundingBox": "250,420,124,59",
  "lines": [
    {
      "boundingBox": "281,420,62,15",
      "words": [
        {
          "boundingBox": "281,420,62,15",
          "text": "BLACK"
        }
      ]
    },
    {
      "boundingBox": "250,441,124,16",
      "words": [
        {
          "boundingBox": "250,441,75,16",
          "text": "FOREST"
        },
        {
          "boundingBox": "331,441,43,16",
          "text": "HAM"
        }
      ]
    },
    {
      "boundingBox": "275,463,73,16",
      "words": [
        {
          "boundingBox": "275,464,53,15",
          "text": "290\/570"
        },
        {
          "boundingBox": "332,463,16,15",
          "text": "cal"
        }
      ]
    }
  ]
}

我想要实现的是提取所有文本值。 所以预期从上面返回的是:(black, forest, ham, 290/570, cal)。

我之前在一个较小的对象上做过这个:

{
  "boundingBox": "275,463,73,16",
  "words": [
    {
     "boundingBox": "275,464,53,15",
     "text": "290\/570"
     },
     {
      "boundingBox": "332,463,16,15",
      "text": "cal"
     }
    ]
 }

我能够使用以下代码实现 (290/570,cal)。

for (x in jsonStruct) {
    $initialValue = "";
    if (typeof(jsonStruct[x]) == "object") {
        //var initialValue = traverseJSON(jsonStruct[x], initialValue);
        var wantedValue = jsonStruct[x];
        for (var i=0;i<wantedValue.length; i++){
            initialValue += wantedValue[i].text +",";
        }
    } else {
        initialValue += x + "->" + jsonStruct[x] + " / ";
    }
}
return initialValue;

但是,在上面列出的较大对象中,我认为由于某些值不是对象,因此代码在第一个不是对象时停止执行。我得到的唯一回应是 boundingBox->250,420,124,59 / 。

那么我该如何进行循环遍历整个对象,返回所有文本值,无论它们是否是对象。他们返回所有文本值这么久?

您的帮助将不胜感激!谢谢!

【问题讨论】:

  • 输入的结构不固定?因为在较大的对象中没有lines,而不是在较小的对象中

标签: javascript loops object return


【解决方案1】:

我相信这会成功:

const obj = {
  "boundingBox": "250,420,124,59",
  "lines": [
    {
      "boundingBox": "281,420,62,15",
      "words": [
        {
          "boundingBox": "281,420,62,15",
          "text": "BLACK"
        }
      ]
    },
    {
      "boundingBox": "250,441,124,16",
      "words": [
        {
          "boundingBox": "250,441,75,16",
          "text": "FOREST"
        },
        {
          "boundingBox": "331,441,43,16",
          "text": "HAM"
        }
      ]
    },
    {
      "boundingBox": "275,463,73,16",
      "words": [
        {
          "boundingBox": "275,464,53,15",
          "text": "290\/570"
        },
        {
          "boundingBox": "332,463,16,15",
          "text": "cal"
        }
      ]
    }
  ]
}

const result = obj.lines.reduce(function(acc, line){
    line.words.forEach(function(word){
        acc.push(word.text));
    };
  return acc;
}, []);

//Or in arrow notation:
 const result = obj.lines.reduce((acc, line) => {
    line.words.forEach(word => acc.push(word.text));
  return acc;
}, []);

console.log(result);
// Prints out ["BLACK", "FOREST", "HAM", "290/570", "cal"]

我使用 reduce 函数,它允许您遍历数组并累积所需的结果。 还要注意箭头符号语法。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2019-12-21
    • 1970-01-01
    • 2023-03-26
    • 2017-04-25
    • 2021-07-21
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 2021-07-16
    相关资源
    最近更新 更多