- wevemob 和 adstard 返回的数据有这样的:
"creatives": {
""cover_url": "https://lh3.googleusercontent.com/YyDDeSsT7sHD-NQcR8l6Rrhki7HSaJ4hmaUzH7ZoXgW4MVafug9zy2T7kFCzKerDt8M"
}
一直以为 creatives.cover_url可以取到值,没想到想错了,根本取不出来。
只能使用 for i,key 使用 creatives[key]可以取到url。
有这样的(一个数组):
creatives=[{
"1200x627": "http://pic.adsoaring.com/creatives/4ce40a1dbc692449bed0970ab3143a7d.JPEG"
},
{
"300x250": "http://pic.adsoaring.com/creatives/b409f7eb250d32b56fb0aefd55eaa798.JPEG"
},
{
"320x480": "http://pic.adsoaring.com/creatives/18ccef98fb963258a8a405d4f1966888.JPEG"
},
{
"320x50": "http://pic.adsoaring.com/creatives/c8f39fc74e5898ad7ad12132269ea28b.JPEG"
},
{
"480x320": "http://pic.adsoaring.com/creatives/9ab865eb0f494ff136f3b49c1a2be1c5.JPEG"
}]
还有这样的(数组又嵌套数组):
creatives=[[{
"728x90": "https://cloud.hugoffers.com/creatives/100001/110067/1516849277266_156.jpg"
}],
{
"1": {
"980x120": "https://cloud.hugoffers.com/creatives/100001/110067/1516849277582_317.jpg"
}
},
{
"2": {
"930x180": "https://cloud.hugoffers.com/creatives/100001/110067/1516849277274_947.jpg"
}
},
{
"5": {
"120x600": "https://cloud.hugoffers.com/creatives/100001/110067/1516849277851_816.jpg"
}
},
{
"9": {
"300x50": "https://cloud.hugoffers.com/creatives/100001/110067/1516849279274_397.jpg"
}
}]
我只能说上游太牛掰了,一个api的数据,一个对象可以有各种数据结构。
-
为了适应上述网盟奇葩的数据结构,写啦如下的递归函数:
function loop_creatives(loop_creatives) {
for (let[i, key]of Object.keys(loop_creatives).entries()) {
if (typeof(loop_creatives[key]) == 'string')
tmpdata.images.push(loop_creatives[key]);
else
arguments.callee(loop_creatives[key]);
}
} -
变体去掉if else语句,如下:
for (let[i, key]of Object.keys(creatives).entries()) {
typeof(creatives[key]) == 'string' && tmpdata.images.push(creatives[key]) || print("no");
} -
如下写法来自最开始的具体写法,目前修改成了错误的(尝试使用小括号表示或语句段,所以错误):
for (let[i, key]of Object.keys(creatives).entries()) {
typeof(creatives[key]) == 'string' && tmpdata.images.push(creatives[key]) || (
for (let[ii, keyi]of Object.keys(creatives[key]).entries()) (
typeof(creatives[key][keyi]) == 'string' && tmpdata.images.push(creatives[key][keyi]) || (
for (let[iii, keyii]of Object.keys(creatives[key][keyi]).entries()) {
typeof(creatives[key][keyi][keyii]) == 'string' && tmpdata.images.push(creatives[key][keyi][keyii]) || console.log("need more for loop");
}
)
)
)
}
- 曾经用过的写法是try catch,因为一直不敢相信 if(ad.creatives.cover_url)其实从未执行过,因为ad.creatives.cover_url根本没有这个属性:
/*
if(ad.creatives.cover_url)
tmpdata.images.push(ad.creatives.cover_url);
else {
console.log(" ad.creatives : " + JSON.stringify(ad.creatives));
try {
ad.creatives.forEach(function(item, index) {
for (let [i, key] of Object.keys(item).entries()) {
if(typeof(item[key])=='string'){
tmpdata.images.push(item[key]);
}
}});
} catch (e) {
console.log("error using wevemob ad.creatives : " + JSON.stringify(ad.creatives));
}
};
*/
- 还用过如下版本的代码:
for (let[i, key]of Object.keys(ad.creatives).entries()) {
if (typeof(ad.creatives[key]) == 'string')
tmpdata.images.push(ad.creatives[key]);
else {
for (let[ii, keyi]of Object.keys(ad.creatives[key]).entries()) {
if (typeof(ad.creatives[key][keyi]) == 'string')
tmpdata.images.push(ad.creatives[key][keyi]);
else {
for (let[iii, keyii]of Object.keys(ad.creatives[key][keyi]).entries()) {
if (typeof(ad.creatives[key][keyi][keyii]) == 'string')
tmpdata.images.push(ad.creatives[key][keyi][keyii]);
else {
console.log("need more for loop");
}
}
}
}
}
}
-
最后在common.js定义了如下函数:
function loop_creatives(loop_creatives,creative_array) {
for (let[i, key]of Object.keys(loop_creatives).entries()) {
typeof(loop_creatives[key]) == 'string' && creative_array.push(loop_creatives[key]) || arguments.callee(loop_creatives[key],creative_array);
}
} -
调用方式为:
Commons.loop_creatives(ad.creatives,tmpdata.images); -
思考:上面的函数传入了一个数组并操作之,且递归第一个元素。
我希望这个函数是传入一个对象并基于该对象返回一个数组,而不需要传入一个数组来操作。
这样,所有的素材对象可以直接调用这个方法来生成了。
网友评论