2016-05-28 3 views

答えて

2

配列が文字列のreduce()

var s1 = '[a,b,c]', 
 
    s2 = '[x,y,z]'; 
 
console.log(
 
    s1 
 
    .slice(1, -1) // remove `[` and `]` 
 
    .split(',') // split based on `,` 
 
    .reduce(function(a, b) { // iterate and generate string 
 
    return a + (a.length ? '\n' : ' ') + b + s2; 
 
    }, '') 
 
)
を使用して取得するには map()

var s1 = '[a,b,c]', 
 
    s2 = '[x,y,z]'; 
 
console.log(
 
    s1 
 
    .slice(1, -1) // remove the `[` and `]` 
 
    .split(',') // split based on `,` 
 
    .map(function(a) { // iterate and generate array 
 
    return a + s2; 
 
    }) 
 
)


を使用して取得するには

var keys = '[a,b,c,d,e]', values = '[x,y,z]', 
    output = keys.slice(1, -1).replace(/([a-z]),?/g, function(m, p1){ 
     return p1 + values + "\n"; 
    }); 

console.log(output); 

出力:String.sliceString.replace機能を使用して

+0

これらの文字列内の項目の 'n' の数が – user4381753

+0

がある場合はどのような@ user4381753:任意の数の文字列でwokします。 –

0
<?php 

$s1 = '[a,b,c]'; 
$s2 = '[x,y,z]'; 

$items = explode(',', trim($s1, '[]')); 
foreach ($items as $item) { 
    echo($item . $s2 . "\n"); 
} 
0

ソリューション

a[x,y,z] 
b[x,y,z] 
c[x,y,z] 
d[x,y,z] 
e[x,y,z] 
関連する問題