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

Bal*_*ian 7 javascript arrays grouping filter react-native

我想将此数据数组过滤为州和城市数组.我怎样才能使用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},
]
Run Code Online (Sandbox Code Playgroud)

用户点击state,状态列表出现.

{state: New York, count: 2},
{state: Texas, count: 1},
{state: Florida, count: 2},
{state: Chicago, count: 1},
Run Code Online (Sandbox Code Playgroud)

当用户单击特定状态时,cities将显示该状态的列表.对于前者 当用户点击纽约州时,

{id:1, name: Mike, city: philps}
{id:3, name: Jhon, city: market}
Run Code Online (Sandbox Code Playgroud)

Mih*_*nut 15

可以做到这一点使用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);
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用arrow函数.

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((item) => item.state == 'New York').map(({id, name, city}) => ({id, name, city}));
console.log(data);
Run Code Online (Sandbox Code Playgroud)


Nin*_*olz 11

使用 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()
);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
Run Code Online (Sandbox Code Playgroud)


小智 7

只需遵循过滤器功能例如

return data.filter(data => data.state == "New York" && count === 2);
Run Code Online (Sandbox Code Playgroud)


Viv*_*ick 6

这种使用是相当简单的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
}, []);
Run Code Online (Sandbox Code Playgroud)

参考这个 jsfiddle来查看它的实际效果。