将对象中的数组转换为单个字符串

时间:2018-12-03 04:22:48

标签: javascript

如何在array中拆分images中的object

例如,使用下面的 JSON 。如何产生每个return中的string itemUrl及其关联的productCode

此JSON

{
  "products": [
    {
      "productCode": "ID1",
      "images": [
        {
          "id": 1,
          "itemUrl": "https://img.com/1.JPG"
        },
        {
          "id": 2,
          "itemUrl": "https://img.com/2.JPG"
        }
      ]
    },
    {
      "productCode": "ID2",
      "images": [
        {
          "id": 3,
          "itemUrl": "https://img.com/3.JPG"
        },
        {
          "id": 4,
          "itemUrl": "https://img.com/4.JPG"
        },
        {
          "id": 5,
          "itemUrl": "https://img.com/5.JPG"
        }
      ]
    }
  ]
}

成为

https://img.com/1.JPG
https://img.com/2.JPG
https://img.com/3.JPG
https://img.com/4.JPG
https://img.com/5.JPG

当前,如果我要使用

for (const tour of data.products) {
  console.log(tour.images[0].itemUrl);
  ...

返回显然会return

https://img.com/1.JPG
https://img.com/3.JPG

但是,何时

let imageEach = tour.images;
let output = [];
imageEach.forEach(item => {
  output.push(item.itemUrl);
});
  ...

我得到return

[{
  https://img.com/1.JPG,
  https://img.com/2.JPG
}]

[{
  https://img.com/3.JPG
  https://img.com/4.JPG
  https://img.com/5.JPG
}]

6 个答案:

答案 0 :(得分:6)

您可以尝试使用Array.reduce来迭代产品,并使用Array.map浏览商品并获得itemUrl

const data = { "products": [ { "productCode": "ID1", "images": [ { "id": 1, "itemUrl": "https://img.com/1.JPG" }, { "id": 2, "itemUrl": "https://img.com/2.JPG" } ] }, { "productCode": "ID2", "images": [ { "id": 3, "itemUrl": "https://img.com/3.JPG" }, { "id": 4, "itemUrl": "https://img.com/4.JPG" }, { "id": 5, "itemUrl": "https://img.com/5.JPG" } ] } ] } 

const result = data.products.reduce((r,{images}) => {
  r.push(...images.map(x => x.itemUrl))
  return r
}, [])
 console.log(result.join('\n'))

或者像@Prassana所建议的那样,通过使用ES6数组将其缩短甚至更多:

const data = { "products": [ { "productCode": "ID1", "images": [ { "id": 1, "itemUrl": "https://img.com/1.JPG" }, { "id": 2, "itemUrl": "https://img.com/2.JPG" } ] }, { "productCode": "ID2", "images": [ { "id": 3, "itemUrl": "https://img.com/3.JPG" }, { "id": 4, "itemUrl": "https://img.com/4.JPG" }, { "id": 5, "itemUrl": "https://img.com/5.JPG" } ] } ] } 

const result = data.products.reduce((r,{images}) => 
  [...r, ...images.map(x => x.itemUrl)], [])

console.log(result.join('\n'))

答案 1 :(得分:0)

尝试

var result = [];
for (var i = 0; i < data.products.length; i++) {
    for (var j = 0; j < data.products[i].images.length; j++){
        result.push(data.products[i].images[j].itemUrl);
    }
}
console.log(result);

答案 2 :(得分:0)

您需要Array.concat

const data = {
  "products": [
    {
      "productCode": "ID1",
      "images": [
        {
          "id": 1,
          "itemUrl": "https://img.com/1.JPG"
        },
        {
          "id": 2,
          "itemUrl": "https://img.com/2.JPG"
        }
      ]
    },
    {
      "productCode": "ID2",
      "images": [
        {
          "id": 3,
          "itemUrl": "https://img.com/3.JPG"
        },
        {
          "id": 4,
          "itemUrl": "https://img.com/4.JPG"
        },
        {
          "id": 5,
          "itemUrl": "https://img.com/5.JPG"
        }
      ]
    }
  ]
}

let urls =  []
data.products.forEach(item => {
  urls = urls.concat(item.images.map(img => img.itemUrl))
})

console.log(urls)

答案 3 :(得分:0)

您可以尝试这个。

let json = {
  "products": [
    {
      "productCode": "ID1",
      "images": [
        {
          "id": 1,
          "itemUrl": "https://img.com/1.JPG"
        },
        {
          "id": 2,
          "itemUrl": "https://img.com/2.JPG"
        }
      ]
    },
    {
      "productCode": "ID2",
      "images": [
        {
          "id": 3,
          "itemUrl": "https://img.com/3.JPG"
        },
        {
          "id": 4,
          "itemUrl": "https://img.com/4.JPG"
        },
        {
          "id": 5,
          "itemUrl": "https://img.com/5.JPG"
        }
      ]
    }
  ]
}
json.products.forEach(product => {
     console.log(product.productCode + ": ")
     product.images.forEach(i => console.log(i.itemUrl))                       
});

// or 
json.products.forEach(product => {
     product.images.forEach(i => console.log(product.productCode + " : " + i.itemUrl))  
});

答案 4 :(得分:0)

products.reduce((urls, product, i) => {
  const imageURLs = product.images.map(img => img.itemUrl);
  urls = urls.concat(imageURLs);
  return urls;
}, []);

尝试一下

答案 5 :(得分:0)

您可以尝试使用我的代码来产生以下结果

{
 "ID1" : ["https://img.com/1.JPG","https://img.com/2.JPG"],
 "ID2" : ["https://img.com/3.JPG","https://img.com/4.JPG","https://img.com/5.JPG"]
}

下面是代码。让您将提到的JSON数据作为“ obj”(变量)

 obj.products.reduce((acc, product)=>{
   acc[product.productcode] = product.images.map(img => img.itemUrl)
   return acc
 }, {})