我有一个对象数组,并且正在使用es6寻找最有效的方法来根据material_id将它们分组
var data=[
{material_id:1, item:'test'},
{material_id:1, item:'test2'},
{material_id:2, item:'test2'},
]
所以最后我想得到
var final_data = [
1,2
]
THat是来自分组的material_id的ID
所以我已经尝试过了,但是在继续进行方面却陷入困境
let final_data = [];
data.forEach(item=>{
//stuck here
})
我检查了This question ,但似乎没有显示分组
答案 0 :(得分:2)
您可以ValueError Traceback (most recent call last)
<ipython-input-72-7fee7a634922> in <module>
----> 1 da_autumn = xr.where(np.isin(da_temp.time.dt.month.values, autumn_mth_list), da_temp, 0)
2 da_autumn.values
~\anaconda3\envs\PlannedBurnWindow\lib\site-packages\xarray\core\computation.py in where(cond, x, y)
1115 join='exact',
1116 dataset_join='exact',
-> 1117 dask='allowed')
~\anaconda3\envs\PlannedBurnWindow\lib\site-packages\xarray\core\computation.py in apply_ufunc(func, input_core_dims, output_core_dims, exclude_dims, vectorize, join, dataset_join, dataset_fill_value, keep_attrs, kwargs, dask, output_dtypes, output_sizes, *args)
967 join=join,
968 exclude_dims=exclude_dims,
--> 969 keep_attrs=keep_attrs)
970 elif any(isinstance(a, Variable) for a in args):
971 return variables_vfunc(*args)
~\anaconda3\envs\PlannedBurnWindow\lib\site-packages\xarray\core\computation.py in apply_dataarray_vfunc(func, signature, join, exclude_dims, keep_attrs, *args)
216
217 data_vars = [getattr(a, 'variable', a) for a in args]
--> 218 result_var = func(*data_vars)
219
220 if signature.num_outputs > 1:
~\anaconda3\envs\PlannedBurnWindow\lib\site-packages\xarray\core\computation.py in apply_variable_ufunc(func, signature, exclude_dims, dask, output_dtypes, output_sizes, keep_attrs, *args)
563 raise ValueError('unknown setting for dask array handling in '
564 'apply_ufunc: {}'.format(dask))
--> 565 result_data = func(*input_data)
566
567 if signature.num_outputs == 1:
~\anaconda3\envs\PlannedBurnWindow\lib\site-packages\xarray\core\duck_array_ops.py in where(condition, x, y)
191 def where(condition, x, y):
192 """Three argument where() with better dtype promotion rules."""
--> 193 return _where(condition, *as_shared_dtype([x, y]))
194
195
~\anaconda3\envs\PlannedBurnWindow\lib\site-packages\xarray\core\duck_array_ops.py in f(*args, **kwargs)
42 else:
43 wrapped = getattr(eager_module, name)
---> 44 return wrapped(*args, **kwargs)
45 else:
46 def f(*args, **kwargs):
ValueError: operands could not be broadcast together with shapes (8760,) (8760,106,193) ()
获取所有ID的数组,然后使用map()
删除重复项
Set
答案 1 :(得分:1)
借助unique函数,我能够像这样解决问题:
const unique = (value, index, self) => {
return self.indexOf(value) === index
}
const data =[
{material_id:1, item:'test'},
{material_id:1, item:'test2'},
{material_id:2, item:'test2'},
];
var final_data = [];
for(var i in data) {
let row = data[i];
final_data.push(row.material_id);
}
final_data = final_data.filter(unique);
console.log(final_data);
答案 2 :(得分:0)
您可以使用reduce
对数组进行分组,并使用Object.keys
获取结果键。
var data=[
{material_id:1, item:'test'},
{material_id:1, item:'test2'},
{material_id:2, item:'test2'},
]
let result = data.reduce((r, a) => {
const {
material_id,
item
} = a;
r[a.material_id] = [...r[a.material_id] || [], {
material_id,
item
}];
return r;
}, {});
console.log(Object.keys(result));