2016-08-09 4 views
0

だから、次のデータを考慮してください。は私に同じ日付を持つ配列内のすべての要素を与える、JS

{ 
    post_views:[ 
     { 
     id:1, 
     post_id:5, 
     ip_address:"xxx", 
     created_at:"2016-08-08T22:22:45+0000", 
     updated_at:"2016-08-08T22:22:45+0000" 
     }, 
     { 
     id:2, 
     post_id:5, 
     ip_address:"yyy", 
     created_at:"2016-08-08T22:23:00+0000", 
     updated_at:"2016-08-08T22:23:00+0000" 
     }, 
     ... 
    ] 
} 

私は一致するすべての要素を取得取得する方法を:2016年8月8日に??

何を試しましたか?

Lodashes、フィルタ:

let datesThatMatch = filter(this.state.data.post_views, (views) => { 
    return moment(views.created_at).format('MMMM Do YYYY') === moment(views.created_at).format('MMMM Do YYYY') 
    }); 

    console.log(datesThatMatch); 

私は気にしないでください彼らはちょうど限り、彼らは試合に作成された日のように、で作成されたその日の何時。

help?

更新

私はちょうど実現私はまた、このすべて間違っについてんだ私はあなたのデータの一部を示したが、これらのオブジェクトの何百もあるので、私の配列(そうでない場合は数千人)と目標ですつまり、2016-08-08の16個の一致、xの日付の50個の一致、yの日付の14個の一致があり、このフィルタの複雑さが増します。

+0

待ち、あなたは 'filter'コールバックを持っているという条件 - それは本当に両側に比べて正確に同じ日付を取得しようとしていますか? – vlaz

+1

これは簡単ですが、どちらも一致しているので、すべて取得してください。 – adeneo

+0

@vld私はあなたの質問を理解していません。私は、指定したフォーマットの日付と一致する配列のすべての要素を取得したいだけです。現在、配列は返ってきたときには空です。 – TheWebs

答えて

2

私はGROUPBYがあなたの更新に基づいて、より良い選択肢だと思う:

var data = { 
 
    post_views:[ 
 
     { 
 
     id:1, 
 
     post_id:5, 
 
     ip_address:"xxx", 
 
     created_at:"2016-08-08T22:22:45+0000", 
 
     updated_at:"2016-08-08T22:22:45+0000" 
 
     }, 
 
     { 
 
     id:2, 
 
     post_id:5, 
 
     ip_address:"yyy", 
 
     created_at:"2016-08-08T22:23:00+0000", 
 
     updated_at:"2016-08-08T22:23:00+0000" 
 
     }, 
 
     { 
 
     id:3, 
 
     post_id:5, 
 
     ip_address:"zzz", 
 
     created_at:"2016-08-12T20:15:00+0000", 
 
     updated_at:"2016-08-12T21:20:00+0000" 
 
     }, 
 
    ] 
 
} 
 
     
 
let groupedDatesThatMatch = _.groupBy(data.post_views, function(views) { 
 
    return moment(views.created_at).format('MMMM Do YYYY') 
 
}); 
 
     
 
console.log(groupedDatesThatMatch);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.2/lodash.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>

あなたはもう一方の端からの配列のオブジェクトを取得

+0

これは正解ですが、それは私にとってはうまくいかず、' this.state。 data.post_views'はオブジェクトの配列を含んでいます。 'groupedDatesThatMatch'は空のオブジェクトに過ぎません。 – TheWebs

+0

私が見た唯一の奇妙なのは、あなたのサンプルではロダッシュを参照していないということでしたが、 'フィルタ'です。あなたは、ロダッシュが宣言されていると確信しています。その後、デバッガを投入する必要があります。またはconsole.logを使用して、関数がフィルタリングしようとしているデータを確認します。 – dmoo

+0

私のエンドでインポートエラーでした。> – TheWebs

0

をあなたが「逆引き参照を構築することができますテーブル "(つまり、各キーの発生場所を示します) サンプルデモは以下のとおりです。

var a = [1,1,2,3,4,5,6,6,7];                               
var reverseIndex = new Map(); 
a.every((cVal, i, a) => { 
    var existing = reverseIndex.has(cVal) ? reverseIndex.get(cVal) : []; 
    existing.push(i); 
    reverseIndex.set(cVal, existing); 
    return true; 
}); 

console.log(reverseIndex); 
0

どの程度...

let post_views = [ 
 
     { 
 
     id:1, 
 
     post_id:5, 
 
     ip_address:"xxx", 
 
     created_at:"2016-08-08T22:22:45+0000", 
 
     updated_at:"2016-08-08T22:22:45+0000" 
 
     }, 
 
     { 
 
     id:2, 
 
     post_id:5, 
 
     ip_address:"yyy", 
 
     created_at:"2016-08-08T22:23:00+0000", 
 
     updated_at:"2016-08-08T22:23:00+0000" 
 
     } 
 
    ] 
 
    
 
let looking_for = new RegExp('2016-08-08') 
 
    
 
let result = post_views.filter(view => looking_for.test(view.created_at) || looking_for.test(view.updated_at)) 
 

 
let summary = result.reduce((prev,view) => { 
 
    
 
        let created_at = view.created_at.split('T')[0]; 
 
        let updated_at = view.updated_at.split('T')[0]; 
 
    
 
         prev.created_at[created_at] = prev.created_at[created_at] || 0; 
 
         prev.updated_at[updated_at] = prev.updated_at[updated_at] || 0; 
 
         prev.created_at[created_at]++; 
 
         prev.updated_at[updated_at]++; 
 
    
 
         return prev; 
 
        },{ created_at: {}, updated_at: {} }) 
 

 
console.log(result); 
 

 
console.log(summary);

0

は、いくつかのfinaglingした後、私はDATEFORMATを使用して、その場で日付をカスタマイズすることを可能にする解決策を考え出すために管理しました簡単に。これはライブラリを使用せず、まっすぐES6を使用します。スニペットを実行すると、日付が一致して保存されていることがわかります。

const arr = { 
 
    post_views:[ 
 
     { 
 
     id:1, 
 
     post_id:5, 
 
     ip_address:"xxx", 
 
     created_at:"2016-08-08T22:22:45+0000", 
 
     updated_at:"2016-08-08T22:22:45+0000" 
 
     }, 
 
     { 
 
     id:2, 
 
     post_id:5, 
 
     ip_address:"yyy", 
 
     created_at:"2016-08-08T22:23:00+0000", 
 
     updated_at:"2016-08-08T22:23:00+0000" 
 
     }, 
 
     { 
 
     id:3, 
 
     post_id:5, 
 
     ip_address:"yyy", 
 
     created_at:"2016-08-09T22:23:00+0000", 
 
     updated_at:"2016-08-08T22:23:00+0000" 
 
     }, 
 
    ] 
 
} 
 
const filterDate = new Date(2016, 08, 08); 
 

 
function parseDate(date) { 
 
    const dateString = date.split("T")[0].split('-'); //Get just the date portion, and then split that into an array. 
 
    const newDate = new Date(Number(dateString[0]), Number(dateString[1]), Number(dateString[2])) //Make the new date object 
 
    return newDate; 
 
} 
 

 
let myMatches = arr.post_views.filter(thisDate => parseDate(thisDate.created_at).getTime() == filterDate.getTime()); 
 

 
console.log(myMatches);

関連する問題