如何过滤native native中的对象数组?

时间:2017-10-21 11:56:27

标签: javascript arrays react-native filter grouping

我想将此数据数组过滤为州和城市数组。我怎样才能使用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}

4 个答案:

答案 0 :(得分:10)

您可以使用native javascript来执行此操作,方法是接受参数 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({id, name, city}){ return {id, name, city}; }); console.log(data);函数。



arrow




答案 1 :(得分:4)

使用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
}, []);

参见this jsfiddle,了解它的实际效果。

答案 2 :(得分:4)

使用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((count, state) => ({ state, count }))
    .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>

答案 3 :(得分:3)

仅遵循过滤器功能 例如

return data.filter(data => data.state == "New York" && count === 2);