var arrNewMarket=[];
var arrMarket=[
{id:1,country:"India",city:"city1",fruit:"Red Apple",year:2019,jan:220,feb:300},
{id:2,country:"India",city:"city1",fruit:"Green Apple",year:2019,jan:777,feb:555},
{id:3,country:"India",city:"city2",fruit:"Red Apple",year:2019,jan:333,feb:888},
]
console.log(arrMarket);
for (let i = 0; i < arrMarket.length-1; i++) {
for (let j = i+1; j < arrMarket.length; j++)
{
if(arrMarket[i].country==arrMarket[j].country && arrMarket[i].city==arrMarket[j].city) {
arrNewMarket.push({
id:4,
country:arrMarket[i].country,
city:arrMarket[i].city,
fruit:"Apple",
year:arrMarket[i].year,
jan:arrMarket[i].jan/arrMarket[j].jan,
feb:arrMarket[i].feb/arrMarket[j].feb,
});
}
}
}
我有多个对象组成的数组。
var arrMarket=[];
[
{id:1,country:India,city:city1,year:2019,jan:220,feb:300},
{id:2,country:India,city:city1,year:2019,jan:777,feb:555},
]
我们可以根据条件创建新对象吗? 例如: 如果国家/地区匹配和城市匹配,那么对于1月和2月进行一些“划分”计算,我们可以得到第三个对象?
任何建议和帮助将不胜感激。
[
{id:1,country:India,city:city1,year:2019,jan:220,feb:300},
{id:2,country:India,city:city1,year:2019,jan:777,feb:555},
{id:3,country:India,city:city1,year:2019,jan:0.28,feb:0.54},
]
答案 0 :(得分:0)
您可以使用lodash filter。
它将允许您挑选出满足条件的数组中的所有对象。
In file included from /usr/include/c++/8/algorithm:62,
from tt.cc:2:
/usr/include/c++/8/bits/stl_algo.h: In instantiation of ‘void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’:
/usr/include/c++/8/bits/stl_algo.h:1885:25: required from ‘void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:1971:31: required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:4866:18: required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = main()::<lambda(const pair_type&, const pair_type&)>]’
tt.cc:13:90: required from here
/usr/include/c++/8/bits/stl_algo.h:1852:17: error: use of deleted function ‘std::pair<const char, int>& std::pair<const char, int>::operator=(const std::pair<const char, int>&)’
*__first = _GLIBCXX_MOVE(__val);
^
In file included from /usr/include/c++/8/utility:70,
from /usr/include/c++/8/algorithm:60,
from tt.cc:2:
/usr/include/c++/8/bits/stl_pair.h:208:12: note: ‘std::pair<const char, int>& std::pair<const char, int>::operator=(const std::pair<const char, int>&)’ is implicitly declared as deleted because ‘std::pair<const char, int>’ declares a move constructor or move assignment operator
struct pair
^~~~
In file included from /usr/include/c++/8/bits/stl_algo.h:61,
from /usr/include/c++/8/algorithm:62,
from tt.cc:2:
/usr/include/c++/8/bits/stl_heap.h: In instantiation of ‘void std::__pop_heap(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’:
/usr/include/c++/8/bits/stl_algo.h:1675:19: required from ‘void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:1933:25: required from ‘void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:1948:27: required from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:1968:25: required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:4866:18: required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = main()::<lambda(const pair_type&, const pair_type&)>]’
tt.cc:13:90: required from here
/usr/include/c++/8/bits/stl_heap.h:252:17: error: use of deleted function ‘std::pair<const char, int>& std::pair<const char, int>::operator=(const std::pair<const char, int>&)’
*__result = _GLIBCXX_MOVE(*__first);
^
In file included from /usr/include/c++/8/algorithm:61,
from tt.cc:2:
/usr/include/c++/8/bits/stl_algobase.h: In instantiation of ‘void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _ForwardIterator2 = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >]’:
/usr/include/c++/8/bits/stl_algo.h:84:20: required from ‘void std::__move_median_to_first(_Iterator, _Iterator, _Iterator, _Iterator, _Compare) [with _Iterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:1921:34: required from ‘_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:1953:38: required from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:1968:25: required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:4866:18: required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = main()::<lambda(const pair_type&, const pair_type&)>]’
tt.cc:13:90: required from here
/usr/include/c++/8/bits/stl_algobase.h:148:11: error: use of deleted function ‘typename std::enable_if<(! std::__and_<std::__is_swappable<_T1>, std::__is_swappable<_T2> >::value)>::type std::swap(std::pair<_T1, _T2>&, std::pair<_T1, _T2>&) [with _T1 = const char; _T2 = int; typename std::enable_if<(! std::__and_<std::__is_swappable<_T1>, std::__is_swappable<_T2> >::value)>::type = void]’
swap(*__a, *__b);
~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/8/utility:70,
from /usr/include/c++/8/algorithm:60,
from tt.cc:2:
/usr/include/c++/8/bits/stl_pair.h:503:5: note: declared here
swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
^~~~
In file included from /usr/include/c++/8/bits/stl_algo.h:61,
from /usr/include/c++/8/algorithm:62,
from tt.cc:2:
/usr/include/c++/8/bits/stl_heap.h: In instantiation of ‘void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Distance = long int; _Tp = std::pair<const char, int>; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’:
/usr/include/c++/8/bits/stl_heap.h:342:22: required from ‘void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:1672:23: required from ‘void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:1933:25: required from ‘void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:1948:27: required from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:1968:25: required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:4866:18: required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = main()::<lambda(const pair_type&, const pair_type&)>]’
tt.cc:13:90: required from here
/usr/include/c++/8/bits/stl_heap.h:225:29: error: use of deleted function ‘std::pair<const char, int>& std::pair<const char, int>::operator=(const std::pair<const char, int>&)’
*(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
^
/usr/include/c++/8/bits/stl_heap.h:231:29: error: use of deleted function ‘std::pair<const char, int>& std::pair<const char, int>::operator=(const std::pair<const char, int>&)’
*(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
^
In file included from /usr/include/c++/8/algorithm:61,
from tt.cc:2:
/usr/include/c++/8/bits/stl_algobase.h: In instantiation of ‘static _BI2 std::__copy_move_backward<true, false, std::random_access_iterator_tag>::__copy_move_b(_BI1, _BI1, _BI2) [with _BI1 = std::pair<const char, int>*; _BI2 = std::pair<const char, int>*]’:
/usr/include/c++/8/bits/stl_algobase.h:588:37: required from ‘_BI2 std::__copy_move_backward_a(_BI1, _BI1, _BI2) [with bool _IsMove = true; _BI1 = std::pair<const char, int>*; _BI2 = std::pair<const char, int>*]’
/usr/include/c++/8/bits/stl_algobase.h:598:5: required from ‘_BI2 std::__copy_move_backward_a2(_BI1, _BI1, _BI2) [with bool _IsMove = true; _BI1 = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _BI2 = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >]’
/usr/include/c++/8/bits/stl_algobase.h:668:48: required from ‘_BI2 std::move_backward(_BI1, _BI1, _BI2) [with _BI1 = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _BI2 = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >]’
/usr/include/c++/8/bits/stl_algo.h:1851:8: required from ‘void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:1885:25: required from ‘void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:1971:31: required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’
/usr/include/c++/8/bits/stl_algo.h:4866:18: required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = main()::<lambda(const pair_type&, const pair_type&)>]’
tt.cc:13:90: required from here
/usr/include/c++/8/bits/stl_algobase.h:548:18: error: use of deleted function ‘std::pair<const char, int>& std::pair<const char, int>::operator=(const std::pair<const char, int>&)’
*--__result = std::move(*--__last);
~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algo.h:61,
from /usr/include/c++/8/algorithm:62,
from tt.cc:2:
/usr/include/c++/8/bits/stl_heap.h:128:5: warning: ‘void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Distance = long int; _Tp = std::pair<const char, int>; _Compare = __gnu_cxx::__ops::_Iter_comp_val<main()::<lambda(const pair_type&, const pair_type&)> >]’ used but never defined
__push_heap(_RandomAccessIterator __first,
^~~~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:71,
from /usr/include/c++/8/algorithm:61,
from tt.cc:2:
/usr/include/c++/8/bits/predefined_ops.h:169:7: warning: ‘__gnu_cxx::__ops::_Iter_comp_val<_Compare>::_Iter_comp_val(__gnu_cxx::__ops::_Iter_comp_iter<_Compare>&&) [with _Compare = main()::<lambda(const pair_type&, const pair_type&)>]’ used but never defined
_Iter_comp_val(_Iter_comp_iter<_Compare>&& __comp)
^~~~~~~~~~~~~~
In file included from /usr/include/c++/8/algorithm:62,
from tt.cc:2:
/usr/include/c++/8/bits/stl_algo.h:1821:5: warning: ‘void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::pair<const char, int>*, std::vector<std::pair<const char, int> > >; _Compare = __gnu_cxx::__ops::_Val_comp_iter<main()::<lambda(const pair_type&, const pair_type&)> >]’ used but never defined
__unguarded_linear_insert(_RandomAccessIterator __last,
^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:71,
from /usr/include/c++/8/algorithm:61,
from tt.cc:2:
/usr/include/c++/8/bits/predefined_ops.h:225:5: warning: ‘__gnu_cxx::__ops::_Val_comp_iter<_Compare> __gnu_cxx::__ops::__val_comp_iter(__gnu_cxx::__ops::_Iter_comp_iter<_Compare>) [with _Compare = main()::<lambda(const pair_type&, const pair_type&)>]’ used but never defined
__val_comp_iter(_Iter_comp_iter<_Compare> __comp)
^~~~~~~~~~~~~~~
希望能给您一些起点。