Several ways to convert arrays into object key-value pairs

1. Array to object

The spread operator(...)

Here's a quick and easy way

const arr = ['one','two','three'];

const obj = {...arr}; console.log(obj); // { 0: 'one', 1: 'tow', 2: 'three' }

Objcet.assign(target, ...sources)

2.foreach

  const classList = {};

  classListItem.forEach((item) => {

    classList[item.id] = item.tagName;

  });

3.reduce

const valueEnumBase = baseList.reduce((p, c) => Object.assign(p, { [c.symbol]: c.symbol }), {})

There are many other methods, these are the most common ones.

Guess you like

Origin blog.csdn.net/chhpearl/article/details/126020900