[英]Objects with data into object with list of keys and values - javascript
I'm struggling with serializing data into a new structure and hope to find a better solution for it.我正在努力将数据序列化为新结构,并希望为其找到更好的解决方案。
I have data that comes from API in this structure:我有来自这种结构的 API 的数据:
const filters = {
category: ['Football', 'Soccer'],
subcategory: ['Tactic', 'Frighting'],
action: ['Kicking', 'Shooting']
};
And I want to convert it into this structure:我想把它转换成这个结构:
const options = {
category: [
{
value: 'Football',
label: 'Football'
},
{
value: 'Soccer',
label: 'Soccer'
}
],
subCategory: [
{
value: 'Tactic',
label: 'Tactic'
},
{
value: 'Frighting',
label: 'Frighting'
}
],
action: [
{
value: 'Kicking',
label: 'Kicking'
},
{
value: 'Shooting',
label: 'Shooting'
}
]
};
Help is appreciated!帮助表示赞赏! Thanks.
谢谢。
Using reduce and map you can loop over the object and transform it.使用 reduce 和 map 您可以遍历对象并对其进行转换。
const filters = { category: ['Football', 'Soccer'], subcategory: ['Tactic', 'Frighting'], action: ['Kicking', 'Shooting'] }; const keyConversion = { subcategory: 'subCategory' } const result = Object.entries(filters).reduce((obj, [key, arr]) => { const mappedKey = keyConversion[key] || key; obj[mappedKey] = arr.map(val => ({ name: val, value: val })); return obj; }, {}); console.log(result);
You could do it like :你可以这样做:
let options = Object.keys(filters).map(key => {
return {
[key]: filters[key].map(filter => {
return {
value: filter,
label: filter
}
})
}
})
const filters = { category: ['Football', 'Soccer'], subcategory: ['Tactic', 'Frighting'], action: ['Kicking', 'Shooting'] } let options = Object.keys(filters).map(key => { return { [key]: filters[key].map(filter => { return { value: filter, label: filter } }) } }) console.log(options)
Use for..in
to iterate over Object
and .map
array helper使用
for..in
迭代Object
和.map
数组助手
const filters = { category: ['Football', 'Soccer'], subcategory: ['Tactic', 'Frighting'], action: ['Kicking', 'Shooting'] }; const result = {} for (key in filters) { result[key] = filters[key].map(v => ({value: v, label: v})) } console.log(result)
You can map the entires of the object to a new [key, value]
entries, where the value is a mapped version of your arrays.您可以将整个对象映射到新的
[key, value]
条目,其中值是数组的映射版本。 Once you have the new [key, value]
pair arrays you can use Object.fromEntries()
to build your resulting object:一旦你有了新的
[key, value]
对数组,你可以使用Object.fromEntries()
来构建你的结果对象:
const filters = {category: ['Football', 'Soccer'], subcategory: ['Tactic', 'Frighting'], action: ['Kicking', 'Shooting'] }; const res = Object.fromEntries(Object.entries(filters).map( ([k, arr]) => [k, arr.map(v => ({value:v, label:v}))] )); console.log(res);
Thanks for everyone, I've used this way finally:谢谢大家,我终于使用了这种方式:
const { ...sportFilters } = queryData;
const filterKeys = keys(sportFilters);
const filterOptions = reduce(
filterKeys,
(acc, key) => {
return {
...acc,
[key]: map(sportFilters[key], value => ({
value,
label: value
}))
};
},
{}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.