繁体   English   中英

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

[英]Convert array in objects to individual strings

如何在object之间分割images array

例如,使用下面的JSON 如何生成每个itemUrl及其关联的productCodereturn string

这个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
}]

您可以通过尝试这样的事情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')) 

尝试

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);

您需要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) 

你可以试试看。

 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)) }); 

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

尝试这个

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

{
 "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
 }, {})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM