代码之家  ›  专栏  ›  技术社区  ›  Darren

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

  •  1
  • Darren  · 技术社区  · 6 年前

    我怎样才能分一杯羹 array 属于 images object ?

    JSON 在下面怎样才能生产出一种新产品 return string itemUrl productCode

    {
      "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);
      ...
    

    返回

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

    但是,什么时候?

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

    返回 属于

    [{
      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 回复  |  直到 6 年前
        1
  •  6
  •   Akrion    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'))
        2
  •  0
  •   holydragon    6 年前

    尝试

    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);
    
        3
  •  0
  •   Prasanna    6 年前

    你需要 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)
        4
  •  0
  •   eag845    6 年前

    你可以试试这个。

    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))  
    });
        5
  •  0
  •   Son Tr.    6 年前
    products.reduce((urls, product, i) => {
      const imageURLs = product.images.map(img => img.itemUrl);
      urls = urls.concat(imageURLs);
      return urls;
    }, []);
    

    试试这个

        6
  •  0
  •   Akki geek    6 年前

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

    {
     "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"]
    }
    

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