I have an array that I pulled from a google sheet. It essentially reports on the performance of my client's Instagram posts. The first column in the array gives an image url and the second column gives a video url depending on if the post was an image or a video. A truncated version of the array will look like this:
[ [ 'Media URL',
'Video thumbnail URL',
'Engagement',
'Media impressions',
'Media reach' ],
[ 'www.sampleimageurl.com/1',
'',
1328,
23359,
22234 ],
[ '',
'www.samplevideourl.com/2',
683,
12542,
11743 ],
[ 'www.sampleimageurl.com/3',
'',
1070,
15232,
11863 ]]
In this array, either the 'Media URL' will have a value in it OR the 'Video thumbnail URL' will have a value in it - there will never be a condition in which either both have a value or none of them have a value.
I need your help to figure out a way to combine the values of the 'Media URL' & 'Video thumbnail URL'. The new array would look something like this:
[ [ 'URL',
'Engagement',
'Media impressions',
'Media reach' ],
[ 'www.sampleimageurl.com/1',
1328,
23359,
22234 ],
[ 'www.samplevideourl.com/2',
683,
12542,
11743 ],
[ 'www.sampleimageurl.com/3',
1070,
15232,
11863 ]]
I've been experimenting with creating an empty array var newArray = []
and filling it up using an if condition with splice but i'm getting nowhere with this approach.
for (i in data) {
var row = data[i]
if (row[1] === "") {
newArray.push(row.splice(1,1))
}
else {
newArray.push(row.splice(0,1))
}
Assuming no other column is empty, You can use filter and map :
const data = [ [ 'Media URL', 'Video thumbnail URL', 'Engagement', 'Media impressions', 'Media reach' ], [ 'www.sampleimageurl.com/1', '', 1328, 23359, 22234 ], [ '', 'www.samplevideourl.com/2', 683, 12542, 11743 ], [ 'www.sampleimageurl.com/3', '', 1070, 15232, 11863 ]]; data[0][0] = "URL"; data[0][1] = ""; const output = data.map(row => row.filter(col => col !=="")); console.info(output);
Short answer:
A.map(item => { item[0] = item[0] || item [1]; item.splice(1, 1); });
A[0][0]="URL";
Live code: https://runkit.com/embed/iixibhqj5sg0
Full code snippet:
const A = [ [ 'Media URL', 'Video thumbnail URL', 'Engagement', 'Media impressions', 'Media reach' ], [ 'www.sampleimageurl.com/1', '', 1328, 23359, 22234 ], [ '', 'www.samplevideourl.com/2', 683, 12542, 11743 ], [ 'www.sampleimageurl.com/3', '', 1070, 15232, 11863 ]]; A.map(item => { item[0] = item[0] || item [1]; item.splice(1, 1); }); A[0][0]="URL"; console.log(A);
function kreatiefMetKurk(){
var array = [ [ 'Media URL',
'Video thumbnail URL',
'Engagement',
'Media impressions',
'Media reach' ],
[ 'www.sampleimageurl.com/1',
'',
1328,
23359,
22234 ],
[ '',
'www.samplevideourl.com/2',
683,
12542,
11743 ],
[ 'www.sampleimageurl.com/3',
'',
1070,
15232,
11863 ]];
array.map(function(row){row[1] = (row[0]+row[1]).trim();
row.shift();
return row;})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.