如何在React Native中过滤对象数组?

问题描述 投票:0回答:5

我想将此数据数组过滤为州和城市数组。我怎样才能使用 lodash 或任何其他更好的方法来实现这一点,而不是 for 循环和维护额外的数组。

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}
javascript arrays react-native filter grouping
5个回答
51
投票

您可以使用

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
函数。

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);


13
投票

使用 lodash,您可以使用

_.filter
和对象作为
_.matches
 iteratee shorthand
来过滤具有给定键/值对的对象,并且

使用

_.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>


11
投票

简单跟随过滤功能 例如

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

7
投票

使用

Array.prototype.filter
Array.prototype.map
Array.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 查看它的实际效果。


0
投票
 try this way    
const users = [
{
  name: 'John Doe',
  number: '123-456-7890',
  address: '123 Main Street, City, Country',
},
{
  name: 'Jane Smith',
  number: '987-654-3210',
  address: '456 Oak Avenue, Town, Country',
},
{
  name: 'Alice Johnson',
  number: '555-123-4567',
  address: '789 Elm Road, Village, Country',
},
];

const [searchText, setSearchText] = useState('');
const [filteredUsers, setFilteredUsers] = useState([]);

const handleSearch = text => {
setSearchText(text);
const filteredData = users.filter(user => {
  const searchTextLowerCase = text.toLowerCase();
  return (
    user.name.toLowerCase().includes(searchTextLowerCase) ||
    user.number.includes(searchText) ||
    user.address.toLowerCase().includes(searchTextLowerCase)
  );
 });

 setFilteredUsers(filteredData);
};

   <TextInput
        placeholder="Search Name Number or Address"
        placeholderTextColor={'#666'}
        style={styles.bottomSheetSearchBar}
        onChangeText={handleSearch}
        value={searchText}
      />
© www.soinside.com 2019 - 2024. All rights reserved.