2016-12-28 6 views
0

私は結合したいいくつかの配列を持っていますが、それらを整理しておく必要があります。 例:2つの配列を並べ替えるにはどうすればいいですか?

var js_files = [ 
'bower_components/jquery/dist/jquery.js', 
'bower_components/jquery.cookie/jquery.cookie.js', 
'bower_components/jquery-placeholder/jquery-placeholder.js', 
'bower_components/foundation-sites/dist/foundation.js', 
'bower_components/Swiper/dist/js/swiper.jquery.min.js', 
'assets/scripts/**/*.js' 
]; 

とは:

var js_files = [ 
'bower_components/jquery/dist/jquery.js', 
'parnet/bower_components/jquery/dist/jquery.js', 
'parent/bower_components/jquery.cookie/jquery.cookie.js' 
]; 

と私は組合にそれらをしたいと拳配列は、最初の&が重複する値を削除し滞在することを確認してください。

期待outotput:

['bower_components/jquery/dist/jquery.js', 
'bower_components/jquery.cookie/jquery.cookie.js', 
'bower_components/jquery-placeholder/jquery-placeholder.js', 
'bower_components/foundation-sites/dist/foundation.js', 
'bower_components/Swiper/dist/js/swiper.jquery.min.js', 
'assets/scripts/**/*.js', 
'parnet/bower_components/jquery/dist/jquery.js', 
'parent/bower_components/jquery.cookie/jquery.cookie.js'] 

私は_.unionを使用してこれを行うことができますか?他のアイデア?

+0

? – tanmay

+2

期待される出力は何ですか? – choz

+0

@tanmay concatは重複した値を処理しません。 –

答えて

1

あなたはArray.prototype.sliceなどを使用してES5の方法でこれを行うことができます:

const first = [1,2,3,4]; 
const second = [5,6,7,8]; 

const results = [].concat(first.slice(), second.slice()); 

またはESNextのファッションを "desctructuring"

const first = [1,2,3,4]; 
const second = [5,6,7,8]; 

const result = [...first, ...second]; 

編集を使用して:私はあなたのポイントを逃しました明確な価値が必要です。

これにより、連結した後の結果から詐欺が削除されます。

const distinctResults = results.filter(function removeDuplicates(item, index){ 

    return results.indexOf(item) === index; 
}); 
+0

' results = first.concat(second) 'だけじゃないの? – choz

+0

@Burimi 'concat'は重複を扱うことができません。 –

0

var a = [1,2,3]; 
 
var b = [4,5,6]; 
 

 
var Joined = [...a,...b];

同様にあなたのコードのために行うことができます。ただArray.prototype.concat

var js_files1 = [ 
 
'bower_components/jquery/dist/jquery.js', 
 
'bower_components/jquery.cookie/jquery.cookie.js', 
 
'bower_components/jquery-placeholder/jquery-placeholder.js', 
 
'bower_components/foundation-sites/dist/foundation.js', 
 
'bower_components/Swiper/dist/js/swiper.jquery.min.js', 
 
'assets/scripts/**/*.js' 
 
]; 
 
var js_files2 = [ 
 
'parnet/bower_components/jquery/dist/jquery.js', 
 
'parent/bower_components/jquery.cookie/jquery.cookie.js' 
 
]; 
 
var res = js_files1.concat(js_files2); 
 
console.log(res);

0

は、あなたの二番目の配列から重複した値を削除します。あなたのケースでは

は、あなたはそれは単に彼らに

をCONCATますので、二番目の配列で重複値を持っていけないしかし、あなたは二番目の配列で重複値を持っている場合、_.unionは、例えばのためにそれを

を削除します。

var a = [ 
    'bower_components/jquery/dist/jquery.js', 
    'bower_components/jquery.cookie/jquery.cookie.js', 
    'bower_components/jquery-placeholder/jquery-placeholder.js', 
    'bower_components/foundation-sites/dist/foundation.js', 
    'bower_components/Swiper/dist/js/swiper.jquery.min.js', 
    'assets/scripts/**/*.js' 
]; 


var b = [ 
    'bower_components/jquery/dist/jquery.js', 
    'parnet/bower_components/jquery/dist/jquery.js', 
    'parent/bower_components/jquery.cookie/jquery.cookie.js' 
]; 

_.union(a, b)あなたは_.unionは、あなたが使用することができ

+0

'concat'は重複を処理できません。 –

0

_.unionを使用

+0

with '_.union'返される配列の順序を制御する方法は?今のところそれはABCによって注文された配列を返します。 –

+0

注文を制御することはできません。要求に応じて別の関数を記述する必要があります。 – Kenny

+0

また、返された配列の順番についても言及してください...今すぐ "_.unionは期待される出力をあなたが問題に言及した – Kenny

0

var js_files1 = [ 
 
'bower_components/jquery/dist/jquery.js', 
 
'bower_components/jquery.cookie/jquery.cookie.js', 
 
'bower_components/jquery-placeholder/jquery-placeholder.js', 
 
'bower_components/foundation-sites/dist/foundation.js', 
 
'bower_components/Swiper/dist/js/swiper.jquery.min.js', 
 
'assets/scripts/**/*.js' 
 
]; 
 

 
var js_files2 = [ 
 
'parnet/bower_components/jquery/dist/jquery.js', 
 
'parent/bower_components/jquery.cookie/jquery.cookie.js', 
 
"assets/scripts/**/*.js" 
 
]; 
 

 
js_files2.forEach(value => { 
 
    if (js_files1.indexOf(value) == -1) { 
 
    js_files1.push(value); 
 
    } 
 
}); 
 

 
console.log(js_files1);

1

結果に重複する値の配列Bの"bower_components/jquery/dist/jquery.js"を削除見ることができます。ここ

[ 
    "bower_components/jquery/dist/jquery.js", 
    "bower_components/jquery.cookie/jquery.cookie.js", 
    "bower_components/jquery-placeholder/jquery-placeholder.js", 
    "bower_components/foundation-sites/dist/foundation.js", 
    "bower_components/Swiper/dist/js/swiper.jquery.min.js", 
    "assets/scripts/**/*.js", 

    "parnet/bower_components/jquery/dist/jquery.js", 
    "parent/bower_components/jquery.cookie/jquery.cookie.js" 
] 

になりますSetと元のソート順のユニークなアイテムの組み合わせはspread syntax ...です。あなただけの `それらをconcat`ない理由

var js_files1 = [ 
 
    'abc', 
 
    'def', 
 
    'bower_components/jquery/dist/jquery.js', 
 
    'bower_components/jquery.cookie/jquery.cookie.js', 
 
    'bower_components/jquery-placeholder/jquery-placeholder.js', 
 
    'bower_components/foundation-sites/dist/foundation.js', 
 
    'bower_components/Swiper/dist/js/swiper.jquery.min.js', 
 
    'assets/scripts/**/*.js' 
 
    ], 
 
    js_files2 = [ 
 
    'def', 
 
    'ghi', 
 
    'bower_components/jquery/dist/jquery.js', 
 
    'parnet/bower_components/jquery/dist/jquery.js', 
 
    'parent/bower_components/jquery.cookie/jquery.cookie.js' 
 
    ], 
 
    uniqueFiles = [...new Set([...js_files1, ...js_files2])]; 
 

 
console.log(uniqueFiles);
.as-console-wrapper { max-height: 100% !important; top: 0; }

関連する問題