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
Add a ment  | 

5 Answers 5

Reset to default 5

You 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;
} , {});

本文标签: