[英]Convert array in objects to individual strings
How can I split an array
of images
among an object
? 如何在
object
之间分割images
array
?
For example, using the JSON below. 例如,使用下面的JSON 。 How can produce a
return
string
of each itemUrl
and it's associated productCode
? 如何生成每个
itemUrl
及其关联的productCode
的return
string
?
This JSON 这个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"
}
]
}
]
}
Becomes 成为
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
Currently, if I were to use 目前,如果我要使用
for (const tour of data.products) {
console.log(tour.images[0].itemUrl);
...
the return would obviously return
回报显然会
return
https://img.com/1.JPG
https://img.com/3.JPG
however, when 但是,当
let imageEach = tour.images;
let output = [];
imageEach.forEach(item => {
output.push(item.itemUrl);
});
...
I get a return
of 我得到了
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
}]
You can try something like this using Array.reduce
to iterate over the products and Array.map
to go through the items and get the itemUrl
: 您可以通过尝试这样的事情
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'))
Or even shorter as suggested by @Prassana by using ES6 array spread some more: 甚至更短,如@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'))
Try 尝试
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);
You need Array.concat
您需要
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)
You could try this. 你可以试试看。
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;
}, []);
Try this 尝试这个
You can try my code to produce following result 您可以尝试我的代码产生以下结果
{
"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"]
}
Below is the code. 下面是代码。 Lets take your mentioned JSON data as "obj"(a variable)
让您将您提到的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.