2017-10-21 105 views
4

我想过滤这个数据数组到状态和城市数组。我如何使用lodash或其他更好的方式来实现这一点,而不是循环和维护额外的数组。如何筛选反应本机中的对象数组?

data: [ 
    { id: 1, name: Mike, city: philps, state: New York}, 
    { id: 2, name: Steve, city: Square, state: Chicago}, 
    { id: 3, name: Jhon, city: market, state: New York}, 
    { id: 4, name: philps, city: booket, state: Texas}, 
    { id: 5, name: smith, city: brookfield, state: Florida}, 
    { id: 6, name: Broom, city: old street, state: Florida}, 
] 

哪个用户点击state,出现状态列表。

{state: New York, count: 2}, 
{state: Texas, count: 1}, 
{state: Florida, count: 2}, 
{state: Chicago, count: 1}, 

当用户点击特定状态时,出现该状态的cities列表。例如。当用户点击纽约州,

{id:1, name: Mike, city: philps} 
{id:3, name: Jhon, city: market} 
+2

的可能的复制[什么是GROUPBY对象的JavaScript的阵列上的最有效的方法?(https://stackoverflow.com/questions/14446511/what-is-the对于状态计数,它的打印输出数据数组是最有效的方法到对象上的一组javascript- – bennygenel

回答

2

随着lodash,你可以使用_.filter与对象为_.matches iteratee速记与给定的键/值对和

使用_.countBy_.map用于获取状态的计数过滤的对象。

var data = [{ id: 1, name: 'Mike', city: 'philps', state: 'New York' }, { id: 2, name: 'Steve', city: 'Square', state: 'Chicago' }, { id: 3, name: 'Jhon', city: 'market', state: 'New York' }, { id: 4, name: 'philps', city: 'booket', state: 'Texas' }, { id: 5, name: 'smith', city: 'brookfield', state: 'Florida' }, { id: 6, name: 'Broom', city: 'old street', state: 'Florida' }]; 
 

 
console.log(_.filter(data, { state: 'New York' })); 
 
console.log(_ 
 
    .chain(data) 
 
    .countBy('state') 
 
    .map((v, k) => ({ state: k, count: v })) 
 
    .value() 
 
);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

+0

。 – Balasubramanian

+0

输出与sinppet中的显示完全一致。但我不一样。 – Balasubramanian

+0

对于第二个控制台。作为结果获取数据 – Balasubramanian

4

可以使用JavaScript的native应用filter方法,其作为参数callback提供的功能做到这一点。

let data= [ 
 
    { id: 1, name: 'Mike', city: 'philps', state:'New York'}, 
 
    { id: 2, name: 'Steve', city: 'Square', state: 'Chicago'}, 
 
    { id: 3, name: 'Jhon', city: 'market', state: 'New York'}, 
 
    { id: 4, name: 'philps', city: 'booket', state: 'Texas'}, 
 
    { id: 5, name: 'smith', city: 'brookfield', state: 'Florida'}, 
 
    { id: 6, name: 'Broom', city: 'old street', state: 'Florida'}, 
 
] 
 
data=data.filter(function(item){ 
 
    return item.state=='New York'; 
 
}).map(function(item){ 
 
    delete item.state; 
 
    return item; 
 
}); 
 
console.log(data);

2

这是相当简单的使用Array.prototype.filterArray.prototype.mapArray.prototype.reduce和解构:

//filter by particular state 
const state = /*the given state*/; 
const filtered = data 
.filter(e => e.state == state)//filter to only keep elements from the same state 
.map(e => { 
    const {id, name, city} = e; 
    return {id, name, city}; 
});//only keep the desired data ie id, name and city 

//get states array 
const states = data 
.reduce((acc, elem) => { 
    const state_names = acc.map(e => e.state);//get all registered names 

    if(state_names.includes(elem.state)){//if it is already there 
    const index = acc.find(e => e.state==elem.state); 
    acc[index] = {state: acc[index].state, count: acc[index].count+1};//increment it's count 
    return acc; 
    }else//otherwise 
    return [...acc, {state: elem.state, count: 1}];//create it 
}, []); 

CF this jsfiddle看到它在行动。