javascript - 嵌套foreach/forloops,循环遍历JSON提取值

标签 javascript json node.js

首先我要说我不确定这是最好的方法,因此非常感谢其他方法

最终目标:通过调用 woocommerce API 并使用响应数据存储产品列表和购买的浇头

我正在调用 woocommerce REST api,它为我提供了大量的 JSON 数据。在 JSON 中是 line_items。这些是购买的产品。嵌套在 line_items 中的是 meta_data,这是例如番茄或酱汁的配料。

附上 JSON 图片 enter image description here

所以我想做的就是创建这样的东西

var testOrderItems =
  [{
      title: "Fried Chicken Burger",
      meta: [
        "Lettuce",
        "cheese slice",
        "kethcup"
      ]
    },
    {
      title: "Beef Burger",
      meta: [
        "Lettuce",
        "cheese slice",
        "kethcup"
      ]
    }
  ]

这将遵循我的订单项目架构

var orderItems = new Schema({
    title: {type: String, required: true},
    meta: [{type: String}]

});

因此,为了做到这一点,我认为我只需通过 JSON 执行 forloop 或 foreach 即可获取所有产品名称及其元数据。获取实际值很容易。困难的部分是创建我可以存储的数组或 JSON 对象,我只是不确定如何在循环中创建它。以下是我尝试过的一些事情

let fullData = JSON.parse(result)

//parsed response from woocommerce API call

fullData.line_items.forEach((product, index) => {
  //for each line item get me the product
  orderItems.push(product.name)
  //var namey = 
  //push the product name to the orderItemsArray
  product.meta_data.forEach(function(meta) {
    //checks for string as one of the plug-ins fills the meta with more nested information and we only want the top level string
    if (typeof meta.value === 'string' || meta.value instanceof String)
      // it's a string
      orderItems.push(meta.value)
    //Onbviously won't nest the meta with the product name just on new lines
  })
});

我认为我可以通过将 ID ref 存储为“i”并能够稍后在嵌套循环中重新引用它以添加元数据来在 for 循环中做到这一点,我对此有点迷茫

var length = fullData.line_items.length

for (let i = 0; i < length; i++) {
  // console.log(i);
  console.log(fullData.line_items[i].name)
  for (let j = 0; j < fullData.line_items[i].meta_data.length; j++) {
    var metaValue = fullData.line_items[i].meta_data[j].value
    if (typeof metaValue === 'string' || metaValue instanceof String) {
     console.log(fullData.line_items[i].meta_data[j].value);
      stringMeta = fullData.line_items[i].meta_data[j].value
      //this works but has drawbacks
      //1 obviously just overwrites itself each time
      //2 will stop at the end of meta so won't add items without meta
      finalOrderItems = {
        id: i,
        name: fullData.line_items[i].name,
        meta: [stringMeta]
      }
    }
  }
}

这就是我所在的地方,感觉这应该非常容易,但目前还不能完全掌握。

最佳答案

您可以先简单地创建代表您的架构的对象,然后从您的 json 对象的映射中返回它。所以,它看起来像下面这样:

let testOrderItems = fullData.line_items.map((product)=>{
     let obj = { name: product.name };
     obj.meta = product.meta_data.map((meta)=>{
         if (typeof meta.value === 'string' || meta.value instanceof String)
          return meta.value;
     }).filter((value)=>!!value);
    return obj;
})
console.log(testOrderItems);

不过,if 语句似乎有点多余,因为 woocommerce api 要么有元数据,要么没有元数据。但是,您可能有一些插件或其他东西可以向元区域添加更多信息,因此我将其保留在我的示例中。

关于javascript - 嵌套foreach/forloops,循环遍历JSON提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51898650/

相关文章:

javascript - 是否可以获取动态加载的javascript文件的下载进度?

javascript - 添加对象数组javascript的值

javascript - 如何在 Node.js 中获取服务器正常运行时间?

node.js - 安装错误 - libcurl 不支持或禁用协议(protocol) "https"

javascript - 如何选择从特定 javascript 类继承的所有自定义元素

javascript - 当有 3 张或更多幻灯片时轮播工作,但只有 2 张幻灯片时会损坏

json - 在 Go 中循环嵌套的 JSON 元素

javascript - npm install 后找不到模块

javascript - 进行多次提取并返回结果的单个异步函数?

javascript - 调用循环中创建的方法始终执行对象的最后一个方法