2017-07-04 3 views
0

私はオブジェクトの配列を取得して、配列の配列でオブジェクトをフィルタリングしようとしています。このよう配列の配列でオブジェクトをフィルタリングする最も効果的な方法は何ですか?

let obj = 
 
{ 
 
    "a.1":1, 
 
    "a.2":2, 
 
    "b.1":3, 
 
    "b.2":4, 
 
    "c.1":5, 
 
    "c.2":6 
 
} 
 

 
let array = 
 
[ 
 
    ["a.1","b.1"], 
 
    ["a"], 
 
    ["b","c.1"] 
 
] 
 

 
let expectedResult = 
 
[ 
 
    { 
 
    "a.1":1, 
 
    "b.1":3, 
 
    }, 
 
    { 
 
    "a.1":1, 
 
    "a.2":2, 
 
    }, 
 
    { 
 
    "b.1":3, 
 
    "b.2":4, 
 
    "c.1":5 
 
    }, 
 
] 
 

 
// this is what I came up with 
 
const filterObjectByArray = (obj, arr) => 
 
    Object.keys(obj) 
 
    .filter(ch => { 
 
     for (var index = 0; index < arr.length; index++) 
 
      if (ch.startsWith(arr[index])) 
 
       return true; 
 
    }) 
 
    .reduce((ret, key) =>{ 
 
     ret[key] = obj[key] 
 
     return ret 
 
    },{}) 
 
    
 
let result = array.map(arr => filterObjectByArray(obj, arr)) 
 

 
//kind of deepEqual 
 
console.log(JSON.stringify(expectedResult) == JSON.stringify(result))

それをするより簡単またはより便利な方法はありますか?私はこの操作をかなり頻繁に行う必要があり、オブジェクトは数百のエントリを大きくすることになりますので、ここで潜在的なボトルネックが発生します。

+0

私は多分正確に、私はそれを行っているだろうかだそれLodash(http://lodash.com/)ユーティリティ – Stratboy

+3

に見てみましょう。追加の速度を絞る必要がある場合は、機能的アプローチを純粋に手続き的に展開してテストすることができます(バニラ 'for'だけで' filter'と 'reduce'を使わずに)。しかし、私はベンチマーク*最初の*を提案して、これが恐れるほど遅いかどうかを確認することもできます(数百のエントリーであれば、毎秒100万回の検索が必要な場合を除き、あなたが知っている、 "時期尚早の最適化はすべての悪の根源です"とそのすべて。 – Amadan

+0

'startsWith' - ' a.15'は 'a.1'で始まります。これはあなたが望むものではないかもしれないと思います。 "(ch +"。 ")startsWith(arr [index] +"。 ")'は信頼性が高くなります( 'a.15.'は' a.'で始まりますが 'a.1'ではありません)。 – Amadan

答えて

0

"基本"(文字)の "実際の"キーへの1種類のマッピングを作成し、それを使ってオブジェクトを作成するときにその文字を実際のキーに変換します。

const obj = { 
 
    "a.1": 1, 
 
    "a.2": 2, 
 
    "b.1": 3, 
 
    "b.2": 4, 
 
    "c.1": 5, 
 
    "c.2": 6 
 
}; 
 

 
const array = [ 
 
    ["a.1", "b.1"], 
 
    ["a"], 
 
    ["b", "c.1"] 
 
]; 
 

 
const getBaseKey = (key) => key.match(/^[a-z]+/)[0]; // get the base of the key - the letter. If it's only one letter, you can use key[0] 
 

 
/** create a one time map of keys by their base **/ 
 
const oobjKeysMap = Object.keys(obj).reduce((map, key) => { 
 
    const baseKey = getBaseKey(key); 
 
    const curr = map.get(baseKey) || []; 
 
    curr.push(key); 
 
    return map.set(baseKey, curr); 
 
}, new Map()); 
 

 
const result = array.map((sub) => // iterate the array 
 
    [].concat(...sub.map((k) => k in obj ? k : oobjKeysMap.get(getBaseKey(k)))) // create the least of "real" keys 
 
    .reduce((ret, key) => { // create the object 
 
    ret[key] = obj[key]; 
 
    return ret; 
 
    }, {}) 
 
); 
 

 
console.log(result);

関連する問題