変更し、JavaScriptの配列を操作します

ジーノ:

ここで私が持っている配列の例は次のとおりです。

var array= ['business>management', 'News>Entertainment News', 'business>Entrepreneurship'];

私はこの結果をしたいです:

['business', 'management', 'News', 'Entertainment News', 'Entrepreneurship']

これは、この「>」重複とは別に、意味します

これは私がでてるところの一例であるが、それは単に矢印「>」削除jsfiddle

norbitrial:

あなたは使用することができますreduce()し、Set例えば組み合わせ。ドキュメントから読みます

Setオブジェクトは、プリミティブ値またはオブジェクト参照かどうかは、どのようなタイプのユニークな値を格納することができます。

reduce()この方法は、単一の出力値が得られ、配列の各要素に(あなたが提供する)減速機能を実行します。

可能な実用的なソリューションをご参照ください。

const array = ['business>management', 'News>Entertainment News', 'business>Entrepreneurship'];

const result = array.reduce((a,c) => {
  c.split('>').forEach(e => a.add(e));
  return a;
}, new Set());

const unique = Array.from(result);

console.log(unique);

私はそれが役に立てば幸い!

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=20022&siteId=1