javascript - 循环遍历对象以获取特定值

标签 javascript loops object return

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

{
  "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/。

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

非常感谢您的帮助!谢谢!

最佳答案

我相信这会完成工作:

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 函数,它允许您迭代一个数组并累积您想要的结果。 另请注意箭头符号语法。

希望这对您有所帮助。

关于javascript - 循环遍历对象以获取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47388909/

相关文章:

javascript - 循环一个 JS 对象

javascript - 如何在 Javascript 中解冻/解冻卡住的对象?

javascript 对象私有(private)属性的行为就像公共(public)属性一样

javascript - 在 AngularJS 中更新屏幕 View

javascript - Express + socket.io + mongoDB 有哪些架构选择

循环内的 JavaScript 闭包 – 简单的实际示例

C++ 对象创建和构造函数

javascript - Jhipster 生成应用程序,默认 AngularJS 注册表单

javascript - 为什么 += 串联运算中的变量会根据其定义方式产生混合结果?

WordPress 显示除一种之外的所有自定义帖子类型