Giter Site home page Giter Site logo

map2arraybykv's Introduction

map2array

map2array

js

将一个map进行按照指定标签进行转化为数组,方便在其他地方使用,我使用在将项目中数据转化结构为and-design中的选择器Picker、CheckBox等中

使用代码:

const map = {
  A: '农、林、牧、渔业',
  A01: '农业',
  A011: '谷物种植',
  A012: '豆类、油料和薯类种植',
  A013: '棉、麻、糖、烟草种植',
};

const map2arrayByKV = (objectMap, optionKeyName = 'label', optionValueName = 'value') => {
  const formatOptions = [];
  if (optionKeyName === optionValueName) return '键名和值名相同,无法进行整理数据';
  /* eslint-disable */
  for (const [value, label] of Object.entries(objectMap)) {
    formatOptions.push({
      [optionKeyName]: label,
      [optionValueName]: value,
    });
  }
  return formatOptions;
};

console.log(map2arrayByKV(map));

转换之前形式如下所示:

const map = {
  A: '农、林、牧、渔业',
  A01: '农业',
  A011: '谷物种植',
  A012: '豆类、油料和薯类种植',
  A013: '棉、麻、糖、烟草种植',
};

转换之后的结果为

[ { label: '农、林、牧、渔业', value: 'A' },
  { label: '农业', value: 'A01' },
  { label: '谷物种植', value: 'A011' },
  { label: '豆类、油料和薯类种植', value: 'A012' },
  { label: '棉、麻、糖、烟草种植', value: 'A013' } ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.