How to filter an array of objects in a native reaction?

I want to filter this array of data in an array of states and cities. How can I achieve this using lodash or any other better way, rather than looping and supporting extra arrays.

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},
]

which the user clicks state, a list of states appears.

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

When the user clicks a specific state, a list of citiesthat state appears . E.g. when a user clicks New York State,

{id:1, name: Mike, city: philps}
{id:3, name: Jhon, city: market}
+4
source share
3 answers

With lodash you can use _.filterwith an object like _.matchesiteratee shorthand to filter an object with a given key / value pair and

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

, native javascript, filter, a 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);
+5

It's quite simple, using Array.prototype.filter, Array.prototype.map, Array.prototype.reduceand destroying:

//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 count
    return acc;
  }else//otherwise
    return [...acc, {state: elem.state, count: 1}];//create it
}, []);

cf this jsfiddle to see it in action.

+3
source

Source: https://habr.com/ru/post/1687935/


All Articles