admin管理员组文章数量:1290945
There is an object array like this:
const schema = [
{ placeholder: 'Title', name: 'title' },
{ placeholder: 'Authors', name: 'author' },
{ placeholder: 'Publisher', name: 'publisher', optional: true },
{ placeholder: 'Edition', name: 'edition', optional: true }
]
Now I would like to get an object with all name
fields as key with 1
value:
result = { 'title': 1, 'author': 1, 'publisher': 1, 'edition': 1 }
I tried to use map
, but
schema.map(o => { return o.name })
gives me just an array:
['title', 'author', 'publisher', 'edition']
There is an object array like this:
const schema = [
{ placeholder: 'Title', name: 'title' },
{ placeholder: 'Authors', name: 'author' },
{ placeholder: 'Publisher', name: 'publisher', optional: true },
{ placeholder: 'Edition', name: 'edition', optional: true }
]
Now I would like to get an object with all name
fields as key with 1
value:
result = { 'title': 1, 'author': 1, 'publisher': 1, 'edition': 1 }
I tried to use map
, but
schema.map(o => { return o.name })
gives me just an array:
['title', 'author', 'publisher', 'edition']
Share
Improve this question
asked Jun 19, 2017 at 11:50
user3142695user3142695
17.4k55 gold badges194 silver badges375 bronze badges
5 Answers
Reset to default 5You need reduce
const schema = [
{ placeholder: 'Title', name: 'title' },
{ placeholder: 'Authors', name: 'author' },
{ placeholder: 'Publisher', name: 'publisher', optional: true },
{ placeholder: 'Edition', name: 'edition', optional: true }
]
console.log(schema.reduce((acc, {name}) => (acc[name] = 1, acc), {}))
const schema = [
{ placeholder: 'Title', name: 'title' },
{ placeholder: 'Authors', name: 'author' },
{ placeholder: 'Publisher', name: 'publisher', optional: true },
{ placeholder: 'Edition', name: 'edition', optional: true }
];
console.log(schema.reduce((acc, current) => {
acc[current.name] = 1;
return acc;
}, {}));
You could use Object.assign
and the spread syntax:
Object.assign(...schema.map(o => ({ [o.name]: 1 })));
const schema = [
{ placeholder: 'Title', name: 'title' },
{ placeholder: 'Authors', name: 'author' },
{ placeholder: 'Publisher', name: 'publisher', optional: true },
{ placeholder: 'Edition', name: 'edition', optional: true }
];
const result = Object.assign(...schema.map(o => ({ [o.name]: 1 })));
console.log(result);
You can first create an object and the use forEach
loop to add properties.
const schema = [
{ placeholder: 'Title', name: 'title' },
{ placeholder: 'Authors', name: 'author' },
{ placeholder: 'Publisher', name: 'publisher', optional: true },
{ placeholder: 'Edition', name: 'edition', optional: true }
]
var obj = {}
schema.forEach(o => obj[o.name] = 1)
console.log(obj)
.map
will always give you an array. Since you want to convert you array of objects to single object, it makes sense to use .reduce
instead.
schema.reduce( (a, c) => {
a[c.name] = 1;
return a;
} , {});
本文标签:
版权声明:本文标题:javascript - JSES6: How to get specific fields of object array and return a single object with specific value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741441782a2378963.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论